python获取目录树_Python读取文件目录树——os.walk
os.walk是Python的內置函數用來遍歷文件目錄樹。
[python]
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)
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)目錄結構為:
[python]
+test
+f1
2.txt
+f2
3.txt
1.txt
+test
+f1
2.txt
+f2
3.txt
1.txt輸出結果為:
[python]
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf1
2.txt
Folder: d:Python Codetestf2
3.txt
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf1
2.txt
Folder: d:Python Codetestf2
3.txt可以看到這是自頂向下的遍歷順序,如果我們要自底向上,先從最深處的文件開始遍歷,可以加上topdown參數:[python] view plaincopyprint?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)
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)結果:
[python]
Folder: d:Python Codetestf1
2.txt
Folder: d:Python Codetestf2
3.txt
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf1
2.txt
Folder: d:Python Codetestf2
3.txt
Folder: d:Python Codetest
1.txt如果想要在搜索的時候加上條件?比如跳過第一個文件夾,os.walk也可以做到:[python] view plaincopyprint?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]
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]結果:
[python]
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf2
3.txt
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf2
3.txt有的同學卻不能得到正確的結果,我們可以看看如下代碼:
[python]
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:]
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:]
結果:
[python]
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf1
2.txt
Folder: d:Python Codetestf2
3.txt
Folder: d:Python Codetest
1.txt
Folder: d:Python Codetestf1
2.txt
Folder: d:Python Codetestf2
3.txt
看起來和之前的版本相似,但是卻不能得到期望的結果。這是因為我們沒有就地改變subdirList的值:
[python]
>> a=[1,2,3]
>>> id(a)
34006600
>>> del a[0]
>>> id(a)
34006600
>>> a = a[1:]
>>> id(a)
34006024
>>> a=[1,2,3]
>>> id(a)
34006600
>>> del a[0]
>>> id(a)
34006600
>>> a = a[1:]
>>> id(a)
34006024
總結
以上是生活随笔為你收集整理的python获取目录树_Python读取文件目录树——os.walk的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: go语言html css,html –
- 下一篇: pycharm变量存_pycharm不为