日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python获取目录树_Python读取文件目录树——os.walk

發布時間:2024/7/5 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。