File size: 2,017 Bytes
d00c95b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d44c90b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d00c95b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import shutil
import hashlib
from tqdm import tqdm


def calculate_hash(file_path):
    # 计算文件的哈希值
    with open(file_path, 'rb') as midi_file:
        content = midi_file.read()
        return hashlib.md5(content).hexdigest()


def mv_duplicates_to_folder(input_folder, output_folder):
    # 创建重复文件夹
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 用于存储文件哈希值的字典
    hash_dict = {}

    # 遍历输入文件夹
    for root, _, files in os.walk(input_folder):
        for file in tqdm(files, desc='Removing duplicates'):
            file_path = os.path.join(root, file)
            file_hash = calculate_hash(file_path)

            # 检查文件哈希值是否已存在
            if file_hash in hash_dict:
                print(f"Duplicates found: {file}")
                # 将重复文件移动到重复文件夹
                destination_path = os.path.join(output_folder, file)
                shutil.move(file_path, destination_path)
            else:
                # 存储文件哈希值
                hash_dict[file_hash] = file_path


def rm_duplicates_in_folder(input_folder):
    # 用于存储文件哈希值的字典
    hash_dict = {}
    duplist = []
    # 遍历输入文件夹
    for root, _, files in os.walk(input_folder):
        for file in tqdm(files, desc='Removing duplicates'):
            file_path = os.path.join(root, file)
            file_hash = calculate_hash(file_path)

            # 检查文件哈希值是否已存在
            if file_hash in hash_dict:
                print(f"Duplicates found: {file}")
                # 将重复文件直接删除
                duplist.append(file_path)
                shutil.rmtree(file_path)
            else:
                # 存储文件哈希值
                hash_dict[file_hash] = file_path

    return duplist


if __name__ == "__main__":
    mv_duplicates_to_folder(input_folder="data", output_folder="duplicates")