之所以多次循环取出结果是由于os.path.walk()的迭代调用回调函数导致的。
3、函数os.walk()
>>> help(os.walk)
Help on function walk in module os:
walk(top, topdown=True, nerror=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. dirnamesis a list ofthe names of the subdirectoriesin 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).
If optional arg 'topdown' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If topdown is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up).
When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune
the search, or to impose a specific order of visiting. Modifying
dirnames when topdown is false is ineffective, since the directories in
dirnames have already been generated by the time dirnames itself is
generated.
By default errors from the os.listdir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an os.error instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.
By default, os.walk does not follow symbolic links to subdirectories on
systems that support them. In order to get this functionality, set the
optional argument 'followlinks' to true.
Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either.
Example:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print root, "consumes",
print sum([getsize(join(root, name)) for name in files]),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
例如:
[oracle@shanxi python]$ moreos_walk.py
import osdef VisitDir(path):for root,dirs,files in os.walk(path):print "%s, root = %s" % (type(root),root)print "%s, dirs = %s" % (type(dirs),dirs)print "%s, files = %s " % (type(files),files)print "\n"for filespath in files:print os.path.join(root,filespath)print "--------------------------"if __name__=="__main__":path="/home/oracle/python"VisitDir(path)
[oracle@shanxi python]$ python os_walk.py
, root = /home/oracle/python, dirs = ['test1', 'test2'], files = ['os_path_walk.py', 'who.py.bak', 'script1.py', 'script1.pyc', 'os_walk.py']/home/oracle/python/os_path_walk.py/home/oracle/python/who.py.bak/home/oracle/python/script1.py/home/oracle/python/script1.pyc/home/oracle/python/os_walk.py--------------------------, root = /home/oracle/python/test1, dirs = [], files = ['who.py']/home/oracle/python/test1/who.py--------------------------, root = /home/oracle/python/test2, dirs = [], files = ['who.py']/home/oracle/python/test2/who.py--------------------------
注:(1)os.walk()返回值是三元组迭代器,“root,dirs,files in os.walk(path)”中的参数dirs纯粹代表目录, files纯粹代表文件,这就在参数位置把文件和目录给区分开来了。而os.path.walk()中的回调函数func的第二个参数,是代表目录和文件的,眉毛胡子一把抓,并没有直接区分目录和文件,当然可以通过进一步判断(函数isfile, isdir)来区分开来。(2 ) os.walk因为返回迭代器所以需要for循环来逐层取出结果,而os.path.walk()不需要循环,是通过单层的回调函数func实现的。