Python遍历文件夹
许多次需要用python来遍历目录下文件, 这一次就整理了记录在这里。
随实际工作,不定期更新。
import os
class FileTraversal:
def __init__(self, rootpath):
self.rootpath = rootpath
#从顶至底的遍历(在剪短的代码里,我比较喜欢这清晰的变量名)
self.tracersal_from_top_to_down = True
#遍历发生错误的时候的回调函数
#函数参数为一个OSError类型参数
#文件名会作为错误参数的一个属性 , 如 error.filename
self.on_error_func = None
#是否变量链接文件(如:软链接、硬链接、windows上的快捷方式)
self.follow_links = False
'''
设置遍历顺序
'''
def setTopToDown(self, from_top_to_dowm=True):
self.tracersal_from_top_to_down = from_top_to_dowm
return self
'''
设置错误回调函数
'''
def setErrorFunc(self, err_func=None):
self.on_error_func = err_func
return self
'''
设置是否遍历连接文件
'''
def setFollowLinks(self, follow_links = False):
self.follow_links = follow_links
return self
'''
获取迭代器
'''
def getGenerator(self):
return os.walk(self.rootpath, self.tracersal_from_top_to_down, self.on_error_func, self.follow_links)
'''
获取所有文件
@param absolute_path: 是否返回绝对路径,或者仅仅文件名
'''
def getFiles(self,absolute_path=True):
files = []
for parent,dirnames,filenames in self.getGenerator(): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for file in filenames:
filepath = os.path.join(parent,file)
files.append( filepath if absolute_path else file)
return files
'''
获取当前目录下所有的文件(不递归遍历)
@
'''
def getThisLevelFiles(self,absolute_path=True):
files = []
all_in_dir = os.listdir(self.rootpath)
for file in all_in_dir:
filepath = os.path.join(self.rootpath, file)
if not os.path.isdir(filepath):
files.append(filepath if absolute_path else file)
return files
一般用法如下:
traversal = FileTraversal("/home/user/testdir")
traversal.setTopToDown(False).setErrorFunc(err_foo).setFollowLinks(True) #不设置,直接使用默认参数亦可
files = traversal.getFiles()
# do something with files ....
源文件: FileTraversal.py
Python遍历文件夹的更多相关文章
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- python遍历文件夹下的文件
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...
- Python 遍历文件夹清理磁盘案例
import os suffix_name_list = [".pdb", ".ilk"] def find_file(path): # 遍历文件夹 for i ...
- python 遍历文件夹下的所有文件
基础 import os # 遍历文件夹 def walkFile(file): for root, dirs, files in os.walk(file): # root 表示当前正在访问的文件夹 ...
- Python遍历文件夹和读写文件的方法
需 求 分 析 1.读取指定目录下的所有文件2.读取指定文件,输出文件内容3.创建一个文件并保存到指定目录 实 现 过 程 Python写代码简洁高效,实现以上功能仅用了40行左右的代码~ 昨天用Ja ...
- python遍历文件夹中所有文件夹和文件,os.walk
python中可以用os.walk来遍历某个文件夹中所有文件夹和文件. 例1: import os filePath = 'C:/Users/admin/Desktop/img' for dirpat ...
- python 遍历文件夹
import os import os.path rootdir = “d:\data” # 指明被遍历的文件夹 for parent,dirnames,filenames in os.walk(ro ...
- python 遍历文件夹中所有文件
'''使用walk方法递归遍历目录文件,walk方法会返回一个三元组,分别是root.dirs和files. 其中root是当前正在遍历的目录路径:dirs是一个列表,包含当前正在遍历的目录下所有的子 ...
- Python遍历文件夹枚举所有文件类型
>>> import os >>> def enumfiles(path, dest): files = os.listdir(path) for f in fil ...
随机推荐
- paip.禁用IKAnalyzer 的默认词库.仅仅使用自定义词库.
paip.禁用IKAnalyzer 的默认词库.仅仅使用自定义词库. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:// ...
- flow.ci + Github + Slack 一步步搭建 Python 自动化持续集成
理想的程序员必须懒惰,永远追随自动化法则.Automating shapes smarter future. 在一个 Python 项目的开发过程中可能会做的事情:编译.手动或自动化测试.部署环境配置 ...
- salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的标签相对简单,如果需要深入了解VF相关知识以及标签, 可以通过以下链接查看或下载 ...
- 转【】浅谈sql中的in与not in,exists与not exists的区别_
浅谈sql中的in与not in,exists与not exists的区别 1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表 ...
- js算法之最常用的排序
引入 大学学习计算机语言的那几年,从c语言,到c++,再到数据结构JAVA..让我印象最深刻的还是最开始老师讲冒泡算法的时候,直到现在大四快毕业了我才渐渐通窍了.刚学前端的时候以为前端就是做出好看很炫 ...
- 第四章 HTML与JavaScript
DHTML就是与CSS和Web文档进行交互生成动态页面的JavaScript. 4.1HTML文档剖析 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...
- Hello Netgen
Hello Netgen eryar@163.com 摘要Abstract:本文主要介绍如何对下载的Netgen源码进行编译生成Netgen程序和程序开发所需要的库nglib. 关键字Key Word ...
- Yii2的深入学习--yii\base\Object 类
之前我们说过 Yii2 中大多数类都继承自 yii\base\Object,今天就让我们来看一下这个类. Object 是一个基础类,实现了属性的功能,其基本内容如下: <?php namesp ...
- 【Spring】Spring AOP实现原理
Spring AOP实现原理 在之前的一文中介绍过Spring AOP的功能使用,但是没有深究AOP的实现原理,今天正好看到几篇好文,于是就自己整理了一下AOP实现的几种方式,同时把代理模式相关知识也 ...
- 常用的Webservice接口
常用的Webservice接口 . 查询手机:http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=手机号码 . 查询IP:h ...