3 回答

TA贡献1921条经验 获得超9个赞
os.walk返回一个迭代器。通常认为要做的就是遍历它
for xroot, dir, file in os.walk('C:\Python27\mycode'): ...
但您也可以只使用xroot, dir, file = next(os.walk('C:\Python27\mycode'))
单步执行

TA贡献1872条经验 获得超4个赞
os.walk不返回root,dir,file。它返回一个生成器对象供程序员循环。很可能是因为给定路径可能包含子目录,文件等。
>>> import os
>>> xroot,dir,file = os.walk('/tmp/') #this is wrong.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> os.walk('/tmp/')
<generator object walk at 0x109e5c820> #generator object returned, use it
>>> for xroot, dir, file in os.walk('/tmp/'):
... print xroot, dir, file
...
/tmp/ ['launched-IqEK']
/tmp/launch-IqbUEK [] ['foo']
/tmp/launch-ldsaxE [] ['bar']
>>>
添加回答
举报