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)