Python工具-文件夹对比

本文介绍了一个简单的Python脚本,用于对比两个文件夹内的文件,并输出共有的文件及其最后修改时间,以及只存在于其中一个文件夹的文件。利用Python的set功能实现了高效比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件夹对比,意思是说对比两个文件夹,看他们里面哪些文件是共有的,哪些是单独所有的。

比如说这里对比d:/a文件夹和d:/b文件夹。最终输出共有的文件【对于共有的文件输出各自的最后修改时间】和单独拥有的文件。

代码如下:

#-*-coding:utf-8-*-  

#===============================================================================
# 目录对比工具,列出
# 1、A比B多了哪些文件
# 2、B比A多了哪些文件
# 3、二者相同的文件,如果最后修改日期不同,则要标注
# 4、包含子目录
#===============================================================================

import os, time

AFILES = []
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 of
if __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)等几个函数可以使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值