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)等几个函数可以使用。