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("./") #此处目录替换成自己电脑微信目录
[Python] 删除电脑微信目录下重复文件
最新推荐文章于 2024-05-18 06:00:00 发布
本文介绍如何使用Python递归遍历文件夹,通过正则表达式筛选出包含特定模式的文件,并利用CRC32校验计算每个文件的校验和。实现过程中,通过移除重复文件并仅保留CRC校验未见的文件,确保文件的完整性和一致性。
摘要由CSDN通过智能技术生成