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

本文介绍了Python内置函数os.walk用于遍历文件目录树的方法,包括自顶向下和自底向上的遍历方式,并展示了如何在遍历过程中添加过滤条件。通过示例代码解释了在遍历过程中修改子目录列表时需要注意的细节问题。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值