Python中 文件目录遍历的 模板 取自python文档
import os, sysfrom stat import *def walktree(top, callback): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pathname) if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) elif S_ISREG(mode): # It's a file, call the callback function callback(pathname) else: # Unknown file type, print a message print 'Skipping %s' % pathnamedef visitfile(file): print 'visiting', fileif __name__ == '__main__': walktree(sys.argv, visitfile)
页:
[1]