首页 > 编程 > Python > 正文

Python利用递归和walk()遍历目录文件的方法示例

2020-01-04 17:00:20
字体:
来源:转载
供稿:网友

前言

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

列出目录结构

python,递归遍历目录,os.walk,遍历,递归遍历文件

一.递归方法

 #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.imlC:/Users/zs/PycharmProjects/demo/.idea/encodings.xmlC:/Users/zs/PycharmProjects/demo/.idea/misc.xmlC:/Users/zs/PycharmProjects/demo/.idea/modules.xmlC:/Users/zs/PycharmProjects/demo/.idea/workspace.xmlC:/Users/zs/PycharmProjects/demo/.ideaC:/Users/zs/PycharmProjects/demo/functiondemo.pyC:/Users/zs/PycharmProjects/demo/index.pyC:/Users/zs/PycharmProjects/demo/locale/en_US/LC_MESSAGES/django.poC:/Users/zs/PycharmProjects/demo/locale/en_US/LC_MESSAGESC:/Users/zs/PycharmProjects/demo/locale/en_USC:/Users/zs/PycharmProjects/demo/locale/zh_CN/LC_MESSAGES/lang.moC:/Users/zs/PycharmProjects/demo/locale/zh_CN/LC_MESSAGES/lang.poC:/Users/zs/PycharmProjects/demo/locale/zh_CN/LC_MESSAGESC:/Users/zs/PycharmProjects/demo/locale/zh_CNC:/Users/zs/PycharmProjects/demo/localeC:/Users/zs/PycharmProjects/demo/name.txtC:/Users/zs/PycharmProjects/demo/text.txt

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

二.os模块的walk()函数

python/205596.html">os.walk(top, topdown=True, onerror=None, followlinks=False)

返回一个3个元素的元祖,(dirpath, dirnames, filenames),

  • dirpath:要列出指定目录的路径
  • dirnames:目录下的所有文件夹
  • filenames:目录下的所有文件

参数一:top – 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径, 文件夹名字, 文件名】。

参数二:topdown –可选,为True或者没有指定, 一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生 (目录自下而上)。

参数三:onerror – 可选,是一个函数; 它调用时有一个参数, 一个OSError实例。报告这错误后,继续walk,或者抛出exception终止walk。

参数四:followlinks – 设置为 true,则通过软链接访问目录。

#coding:utf-8import osdef getallfiles(path): allfile=[] for dirpath,dirnames,filenames in os.walk(path):  for dir in dirnames:   allfile.append(os.path.join(dirpath,dir))  for name in filenames:   allfile.append(os.path.join(dirpath, name)) return allfileif __name__ == '__main__': path = "C:/Users/zs/PycharmProjects/demo" allfile=getallfiles(path) for file in allfile:  print file#输出结果C:/Users/zs/PycharmProjects/demo/.ideaC:/Users/zs/PycharmProjects/demo/localeC:/Users/zs/PycharmProjects/demo/functiondemo.pyC:/Users/zs/PycharmProjects/demo/index.pyC:/Users/zs/PycharmProjects/demo/name.txtC:/Users/zs/PycharmProjects/demo/text.txtC:/Users/zs/PycharmProjects/demo/.idea/demo.imlC:/Users/zs/PycharmProjects/demo/.idea/encodings.xmlC:/Users/zs/PycharmProjects/demo/.idea/misc.xmlC:/Users/zs/PycharmProjects/demo/.idea/modules.xmlC:/Users/zs/PycharmProjects/demo/.idea/workspace.xmlC:/Users/zs/PycharmProjects/demo/locale/en_USC:/Users/zs/PycharmProjects/demo/locale/zh_CNC:/Users/zs/PycharmProjects/demo/locale/en_US/LC_MESSAGESC:/Users/zs/PycharmProjects/demo/locale/en_US/LC_MESSAGES/django.poC:/Users/zs/PycharmProjects/demo/locale/zh_CN/LC_MESSAGESC:/Users/zs/PycharmProjects/demo/locale/zh_CN/LC_MESSAGES/lang.moC:/Users/zs/PycharmProjects/demo/locale/zh_CN/LC_MESSAGES/lang.po

小结

两种方法输入结果是一样的。但是在输入的顺序有差异。对比感觉python/86831.html">python的os模块的walk()方法比较简单

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对VEVB武林网的支持。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表