python对比并输出多文件夹内的不同文件
python对比并输出多文件夹内的不同文件。有时在做一些测试时会遇到许多很相似的文件夹,但其内部的文件却相差无几,用眼睛去看多半会出错。用一个程序能完成这项工作是最好的结果了。下面这段代码就可以解决这个问题。
对于python文件操作不太了解的朋友,可以关注:
Python遍历文件夹目录与文件操作
Python open()函数文件打开、读、写操作详解
Python 文件对象常用内建方法
# coding=gbk import os,ConfigParser ''' 递归列出某目录下的文件,放入List中 ''' def listDir (fileList,path): files=os.listdir(path) for i in files: file_path=path+"\\"+i if os.path.isfile(file_path): fileList.append(file_path) for i in files: file_path=path+"\\"+i if os.path.isdir(file_path): #fileList.append(file_path) listDir(fileList,file_path) return fileList ''' 将List中内容写入文件 www.iplaypy.com ''' def writeListToFile(list,path): strs="\n".join(list) f=open(path,'wb') f.write(strs) f.close() ''' 读入文件内容并放入List中 ''' def readFileToList(path): lists=[] f=open(path,'rb') lines=f.readlines() for line in lines: lists.append(line.strip()) f.close() return lists ''' 比较文件--以Set方式 ''' def compList(list1,list2): return list(set(list1)-set(list2)) ''' 复制List中文件到指定位置 ''' def copyFiles(fileList,targetDir): for file in fileList: targetPath=os.path.join(targetDir,os.path.dirname(file)) targetFile=os.path.join(targetDir,file) if not os.path.exists(targetPath): os.makedirs(targetPath) if not os.path.exists(targetFile) or (os.path.exists(targetFile) and os.path.getsize(targetFile)!=os.path.getsize(file)): print "正在复制文件:"+file open(targetFile,'wb').write(open(file,'rb').read()) else: print "文件已存在,不复制!" if __name__ == '__main__': path=".svn" #获取源目录 txtFile="1.txt" #目录结构输出的目的文件 tdir="cpfile" #复制到的目标目录 cfFile="config.ini"; #配置文件文件名 fileList=[] #读取配置文件 if(os.path.exists(cfFile)): cf=ConfigParser.ConfigParser() cf.read(cfFile) path=cf.get("main", "sourceDir") txtFile=cf.get("main","txtFile") tdir=cf.get("main","targetDir") else: print "配置文件不存在!" raw_input("\n按 回车键 退出\n") exit() if(os.path.exists(txtFile)): #如果导出的文件存在,就读取后比较 list1=readFileToList(txtFile) print "正在读取文件列表……" fileList=listDir (fileList,path) print "正在比较文件……" list_res=compList(fileList,list1) if len(list_res)>0: print "以下是原目录中不存在的文件:\n" print "\n".join(list_res) print "\n共计文件数:"+str(len(list_res))+"\n" if raw_input("\n是否复制文件?(y/n)")!='n': copyFiles(list_res,tdir) else: print "没有不相同的文件!" else: #如果导出的文件不存在,则导出文件 print "正在读取文件列表……" fileList=listDir (fileList,path) writeListToFile(fi 2000 leList,txtFile) print "已保存到文件:"+txtFile raw_input("\n按 回车键 退出\n")
python对比并输出多文件夹内的不同文件
#配置文件名:config.ini [main] sourceDir=wwwroot txtFile=1.txt targetDir=cp
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/scripts-shell/ss2517.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • 绑定修改网卡绑定关系的python程序
- • 网友用python把IPv4地址变成LITNET-NAT64网段
- • 基于python Selenium的用户登录自动化测试
- • 用python代码科学上Google
- • python论坛自动签到用bs4模块
- • python北京地铁月支出简易计算器
- • 用python查找未注册的域名
- • Windows环境用Python备份MySQL脚本
- • 批量调整图片尺寸的python脚本
- • Python方法获取百度地图数据示例源码
图文精华 RECOMMEND
-
用python代码科学上Google
-
Windows环境用Python备份MySQL脚本
-
批量调整图片尺寸的python脚本
-
Python方法获取百度地图数据示例源
-
控制台进度自动刷新python方法源码
-
web.py能条件判断的页面执行计时方
热点文章 HOT
- web.py能条件判断的页面执行计时方法
- Windows环境用Python备份MySQL脚本
- 从糗事百科下载数据的python方法示例
- 基于python Selenium的用户登录自动化测试
- 解决Python2不支持datetime的json encode问题
- Python完成抓取并写入mysql库的方法
- 一个初学者练手的Python多线程实现下载的
- Python调chrome刷页面完成刷点击量操作