Python工具-文件夹对比
来源:互联网 发布:int范围 java 编辑:程序博客网 时间:2024/04/28 06:13
文件夹对比,意思是说对比两个文件夹,看他们里面哪些文件是共有的,哪些是单独所有的。
比如说这里对比d:/a文件夹和d:/b文件夹。最终输出共有的文件【对于共有的文件输出各自的最后修改时间】和单独拥有的文件。
代码如下:
#-*-coding:utf-8-*- #===============================================================================# 目录对比工具,列出# 1、A比B多了哪些文件# 2、B比A多了哪些文件# 3、二者相同的文件,如果最后修改日期不同,则要标注# 4、包含子目录#===============================================================================import os, timeAFILES = []BFILES = []COMMON = []def getPrettyTime(state): return time.strftime('%y-%m-%d %H:%M:%S', time.localtime(state.st_mtime))def dirCompare(apath, bpath): afiles = [] bfiles = [] for root, dirs , files in os.walk(apath): for f in files : afiles.append(root + "\\" + f) for root, dirs , files in os.walk(bpath): for f in files : bfiles.append(root + "\\" + f) #=========================================================================== # 去掉afiles中文件名的apath #=========================================================================== apathlen = len(apath) aafiles = [] for f in afiles: aafiles.append(f[apathlen:]) #=========================================================================== # 去掉afiles中文件名的apath #=========================================================================== bpathlen = len(bpath) bbfiles = [] for f in bfiles: bbfiles.append(f[bpathlen:]) afiles = aafiles bfiles = bbfiles setA = set(afiles) setB = set(bfiles) #=========================================================================== # 处理共有文件 #=========================================================================== commonfiles = setA & setB print "#=================================" print "common in '", apath, "' and '", bpath, "'" print "#=================================" print '\t\t\ta:\t\t\t\t\t\tb:' for f in sorted(commonfiles): print f + "\t\t" + getPrettyTime(os.stat(apath + "\\" + f)) + "\t\t" + getPrettyTime(os.stat(bpath + "\\" + f)) #=========================================================================== # 处理仅出现在一个目录中的文件 #=========================================================================== onlyFiles = setA ^ setB aonlyFiles = [] bonlyFiles = [] for of in onlyFiles: if of in afiles: aonlyFiles.append(of) elif of in bfiles: bonlyFiles.append(of) print "#=================================" print "#only in ", apath print "#=================================" for of in sorted(aonlyFiles): print of print "#=================================" print "#only in ", bpath print "#=================================" for of in sorted(bonlyFiles): print ofif __name__ == '__main__': dirCompare('d:/a', 'd:/b')
运行结果如下:
其实实现起来还蛮简单的,因为python自身太强大。主要是用了python的set的功能。
a = t | s # t 和 s的并集
b = t & s # t 和 s的交集
c = t – s # 求差集(项在t中,但不在s中)
d = t ^ s # 对称差集(项在t或s中,但不会同时出现在二者中)
----------------------------
在python中实际上本来就有这样的工具。filecmp模块就是做这个工作的。
filecmp模块有cmp(file1,file2)、cmpfiles(dir1,dir2)、dircmp(dir1,dir2)等几个函数可以使用。
- Python工具-文件夹对比
- 文件夹对比工具_JAVA实现
- 文档、文件夹对比工具 BC2
- Python 打包工具对比,Nuitka vs Pyinstaller
- 【实用软件分享02】文本代码对比合并,文件夹对比合并工具:Beyond Compare
- 对比工具
- python安装第三方的包 工具对比
- 使用Python实现一个文本对比报告生成工具
- python安装第三方的包 工具对比
- 文件夹对比软件
- 对比文件或者文件夹不同的工具——Beyond Compare
- 对比java和python对比
- meld 工具 ubuntu 对比工具
- SCM工具对比分析
- 数据库设计工具对比
- SCM工具对比分析
- 数据库设计工具对比
- 对比工具compare
- Oracle新建一个可以访问其他用户的表结构的用户
- hadoop搭建教程之三(Hbase伪分布安装)
- linux下安装jdk、tomcat、eclipse、myeclipse
- [NOI2008]糖果雨——数形结合的思想
- google reader 的停用
- Python工具-文件夹对比
- 4clojure第65个问题:黑盒测试序列类型
- python(1)白猫黑猫
- 浅析Linux下core文件
- linux apt命令
- Android的Handler总结
- ENVI 5.0 32 64位 下载
- sudo命令
- POJ-1847-Tram