python下递归遍历目录和文件的方法介绍


本文摘自php中文网,作者零下一度,侵删。

在日常开发中经常需要检查一个“目录或文件夹”内部有没有我们想要的文件或者文件夹,下面这篇文章主要给大家介绍了关于Python利用递归和walk()遍历目录文件的相关资料。

方法一:递归调用:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

[html] view plain copy

#!/usr/bin/python 

#coding:utf8 

   

import os 

   

def dirlist(path, allfile): 

    filelist =  os.listdir(path) 

   

    for filename in filelist: 

        filepath = os.path.join(path, filename) 

        if os.path.isdir(filepath): 

            dirlist(filepath, allfile) 

        else: 

            allfile.append(filepath) 

    return allfile 

   

print dirlist("/home/yuan/testdir", [])


方法二:

os.walk()

该函数式穿件一个生成器对象来遍历整棵目录树。
top指定目录的顶级,而topdown是一个布尔值,用于指示由上而下(默认值)还是由下而上来遍历目录。返回的生成器将生成元组(dirpath,dirnames,filenames),其中dirpath是一个字符串,包含通向目录的路径,dirnames是dirpath中所有子目录的一个列表,而filename是dirpath中文件的一个列表,不包括目录。oneerror参数是一个接受单个参数的函数。
如果处理期间出现任何错误,将使用os.error的是咧来调用此函数。默认行为时忽略错误。如果由上而下地遍历目录,修改dirnames将影响到遍历过程。

1

2

3

4

5

6

7

8

9

10

11

12

[python] view plain copy

#!/user/bin/python 

#!conding=utf8 

   

import os 

g = os.walk("/home/yuan/testdir") 

   

   

for path,d,filelist in g: 

    print d; 

    for filename in filelist: 

        print os.path.join(path, filename)

经常需要检查一个“目录或文件夹”内部有没有我们想要的文件或者文件夹,就需要我们循环迭代出所有文件和子文件夹,Python中遍历指定目录下所有的文件和文件夹,包含多级目录,有两种方法,一种是通过递归思想去遍历,另一种是os模块的walk()函数下面话不多说,就来一起看看详细的介绍:

列出目录结构

一.递归方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

#coding:utf-8

 import os

 allfile=[]

 def getallfile(path):

  allfilelist=os.listdir(path)

  for file in allfilelist:

   filepath=os.path.join(path,file)

   #判断是不是文件夹

   if os.path.isdir(filepath):

    getallfile(filepath)

   allfile.append(filepath)

  return allfile

 

 if name == 'main':

 

  path="C:\Users\zs\PycharmProjects\demo"

  allfiles=getallfile(path)

 

  for item in allfiles:

   print item

 

 

#结果

C:\Users\zs\PycharmProjects\demo\.idea\demo.iml

C:\Users\zs\PycharmProjects\demo\.idea\encodings.xml

C:\Users\zs\PycharmProjects\demo\.idea\misc.xml

C:\Users\zs\PycharmProjects\demo\.idea\modules.xml

C:\Users\zs\PycharmProjects\demo\.idea\workspace.xml

C:\Users\zs\PycharmProjects\demo\.idea

C:\Users\zs\PycharmProjects\demo\functiondemo.py

C:\Users\zs\PycharmProjects\demo\index.py

C:\Users\zs\PycharmProjects\demo\locale\en_US\LC_MESSAGES\django.po

C:\Users\zs\PycharmProjects\demo\locale\en_US\LC_MESSAGES

C:\Users\zs\PycharmProjects\demo\locale\en_US

C:\Users\zs\PycharmProjects\demo\locale\zh_CN\LC_MESSAGES\lang.mo

C:\Users\zs\PycharmProjects\demo\locale\zh_CN\LC_MESSAGES\lang.po

C:\Users\zs\PycharmProjects\demo\locale\zh_CN\LC_MESSAGES

C:\Users\zs\PycharmProjects\demo\locale\zh_CN

C:\Users\zs\PycharmProjects\demo\locale

C:\Users\zs\PycharmProjects\demo\name.txt

C:\Users\zs\PycharmProjects\demo\text.txt

借用递归的思想去列出所有文件夹中的内容,判断如果是目录就继续调用本身的方法。

阅读剩余部分

相关阅读 >>

Python文件存储路径如何使用变量

Python3.6.4如何安装到树莓派3代

怎么保存Python代码?

Python实现的根据文件名查找数据文件功能示例

Python实现有序字典方法示例

Python中sep是什么意思

Python怎么创建类

Python绝对值怎么计算

Python可以在手机上运行吗

Python中int的用法是什么

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...