help(os.walk)
walk(top, topdown=True, onerror=None, followlinks=False)
Directory tree generator.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple
dirpath, dirnames, filenames
dirpath is a string, the path to the directory.
dirnames is a list ofthe names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name).
函数声明:walk(top,topdown=True,onerror=None)
1>参数top表示需要遍历的目录树的路径
2>参数topdown的默认值是"True",表示首先返回目录树下的文件,然后在遍历目录树的子目录.Topdown的值为"False"时,则表示先遍历目录树的子目录,返回子目录下的文件,最后返回根目录下的文件
3>参数onerror的默认值是"None",表示忽略文件遍历时产生的错误.如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历
4>该函数返回一个元组,该元组有3个元素,这3个元素分别表示'每次遍历的路径名,子目录列表和文件列表',只遍历目录
测试:
>>> os.system('mkdir /loc/{beijing,shanghai,henan} -p')
0
>>> os.system('touch /loc/beijing.txt,shanghai.txt,henan.txt')
0
>>> os.system('touch /loc/beijing/bj.txt')
0
>>> os.system('touch /loc/shanghai/sh.txt')
0
>>> os.system('touch /loc/henan/hn.txt')
0
>>> for root,dir,file in os.walk('/loc'):
... print root
... for d in dir:
... print d
... for fn in file:
... print fn
...
/loc --root
shanghai --dir
beijing
henan
beijing.txt,shanghai.txt,henan.txt --file
/loc/shanghai --root no sundir
sh.txt --file
/loc/beijing
bj.txt
/loc/henan
hn.txt
>>> for root,dir,file in os.walk('/loc'):
... print root
...
/loc
/loc/shanghai
/loc/beijing
/loc/henan
>>> for root,dir,file in os.walk('/loc'):
... for d in dir:
... print d
...
shanghai
beijing
henan
>>> for root,dir,file in os.walk('/loc'):
... for fn in file:
... print fn
...
{beijing.txt,shanghai.txt,henan.txt
sh.txt
bj.txt
hn.txt
-------------------------------------------
help(os.path.walk)
walk(top, func, arg)
Directory tree walk with callback function.
For each directory in the directory tree rooted at top (including topitself, but excluding '.' and '..'),
call func(arg, dirname, fnames).
dirname is the name of the directory
fnames a list of the names of the files and subdirectories in dirname (excluding '.' and '..').
func may modify the fnames list in-place (e.g. via del or slice assignment), and walk will only recurse into the subdirectories whose names remain in fnames; this can be used to implement a filter, or to impose a specific order of visiting. No semantics are defined for, or required of,
arg,beyond that arg is always passed to func. It can be used, e.g., to pass a filename pattern, or a mutable object designed to accumulate statistics. Passing None for arg is common.
1>参数top表示需要遍历的目录树的路径
2>参数func表示回调函数,对遍历路径进行处理.所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务.回调函数必须提供3个参数:第1个参数为walk()的参数tag,第2个参数表示目录列表,第3个参数表示文件列表
3>参数arg是传递给回调参数func的元组.回调函数的一个参数必须是arg,为回调函数提供处理参数.参数arg可以为空
os.path.walk()与os.walk()产生的文件名列表并不相同.os.path.walk()产生目录树下的目录路径和文件路径,而os.walk()只产生文件路径
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29974949/viewspace-1572276/
,如需转载,请注明出处,否则将追究法律责任。