通过hash判断相同简历文件,并将重复文件移动至其他目录

import os
import hashlib
import shutil

def calculate_hash(file_path):
    """计算文件的SHA-256哈希值"""
    sha256 = hashlib.sha256()
    with open(file_path, 'rb') as f:
        for block in iter(lambda: f.read(4096), b""):
            sha256.update(block)
    return sha256.hexdigest()

def move_duplicates(dir_path, dest_path):
    """递归遍历目录,查找重复文件并移动"""
    if not os.path.exists(dest_path):
        os.makedirs(dest_path)
    
    file_hashes = {}
    
    for root, _, files in os.walk(dir_path):
        for file in files:
            file_path = os.path.join(root, file)
            file_hash = calculate_hash(file_path)
            
            if file_hash in file_hashes:
                # 如果文件哈希值已经存在,说明是重复文件,移动到目标目录
                shutil.move(file_path, os.path.join(dest_path, file))
                print(f"重复文件 {file_path} 已移动到 {dest_path}")
            else:
                # 否则,记录文件哈希值
                file_hashes[file_hash] = file_path

dir_path = "/Users/北京橙溪科技/Documents/EnWing.com/allResume/"
dest_path = "/Users/北京橙溪科技/Downloads/del/"

move_duplicates(dir_path, dest_path)

上一篇: java php nodejs asp.net python后台开发语言如何选择?
下一篇: 一份程序员13年前的年终总结,那年大学刚毕业,方正也在巅峰期
hellochenlian
博客等级 码龄17年 629粉丝 295原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

hellochenlian

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

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

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

打赏作者

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

抵扣说明:

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

余额充值