[Python] 删除电脑微信目录下重复文件

本文介绍如何使用Python递归遍历文件夹,通过正则表达式筛选出包含特定模式的文件,并利用CRC32校验计算每个文件的校验和。实现过程中,通过移除重复文件并仅保留CRC校验未见的文件,确保文件的完整性和一致性。
摘要由CSDN通过智能技术生成
import os
import zlib
import time
import re
#递归文件夹
def recursionFolder(path):
    filename_regex = re.compile(r'.*\(\d+\)\..*')
    crcList=list()
    for root,dirs,files in os.walk(path):
        for file in files:
            print(os.path.join(root,file))
            temp = filename_regex.search(file)
            if temp != None:
                os.remove(os.path.join(root,file))
            if os.path.exists((os.path.join(root,file))):
                fileCRC=crc32(os.path.join(root,file))
                if fileCRC in crcList:
                    os.remove(os.path.join(root,file))
                else:
                    crcList.append(fileCRC)
#获取文件crc值
def crc32(file_path):
    with open(file_path, 'rb') as fh:
        hash = 0
        while True:
            s = fh.read(65536)
            if not s:
                break
            hash = zlib.crc32(s, hash)
        return "%08X" % (hash & 0xFFFFFFFF)
recursionFolder("./")     #此处目录替换成自己电脑微信目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞天的大鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值