os.walk是Python的内置函数用来遍历文件目录树。
看起来和之前的版本相似,但是却不能得到期望的结果。这是因为我们没有就地改变subdirList的值:
import os
rootDir = 'd:\\assa'
for dirName, subdirList, fileList in os.walk(rootDir):
print('Folder: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
目录结构为:
+test
+f1
2.txt
+f2
3.txt
1.txt输出结果为:Folder: d:\Python Code\test
1.txt
Folder: d:\Python Code\test\f1
2.txt
Folder: d:\Python Code\test\f2
3.txt可以看到这是自顶向下的遍历顺序,如果我们要自底向上,先从最深处的文件开始遍历,可以加上topdown参数:import os
rootDir = 'd:\\Python Code\\test'
for dirName, subdirList, fileList in os.walk(rootDir, topdown = False):
print('Folder: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)结果:Folder: d:\Python Code\test\f1
2.txt
Folder: d:\Python Code\test\f2
3.txt
Folder: d:\Python Code\test
1.txt如果想要在搜索的时候加上条件?比如跳过第一个文件夹,os.walk也可以做到:import os
rootDir = 'd:\\Python Code\\test'
for dirName, subdirList, fileList in os.walk(rootDir):
print('Folder: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
if len(subdirList) > 0:
del subdirList[0]结果:Folder: d:\Python Code\test
1.txt
Folder: d:\Python Code\test\f2
3.txt有的同学却不能得到正确的结果,我们可以看看如下代码:import os
rootDir = 'd:\\Python Code\\test'
for dirName, subdirList, fileList in os.walk(rootDir):
print('Folder: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
if len(subdirList) > 0:
subdirList = subdirList[1:]
结果:
Folder: d:\Python Code\test
1.txt
Folder: d:\Python Code\test\f1
2.txt
Folder: d:\Python Code\test\f2
3.txt看起来和之前的版本相似,但是却不能得到期望的结果。这是因为我们没有就地改变subdirList的值:
>>> a=[1,2,3]
>>> id(a)
34006600
>>> del a[0]
>>> id(a)
34006600
>>> a = a[1:]
>>> id(a)
34006024
本文介绍了Python中使用os.walk遍历文件目录树的方法,详细解析了os.walk的工作原理及其在处理复杂目录结构时的应用示例。
5956

被折叠的 条评论
为什么被折叠?



