-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (82 loc) · 3.33 KB
/
main.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import sys
import shutil
from PIL import Image
import imagehash
import filecmp
def get_file_hash(file_path):
"""Calculate the hash of a file."""
with open(file_path, 'rb') as f:
return hash(f.read())
def identify_duplicates(folder_path):
"""Identify duplicate files in a folder based on their hashes."""
print("Scanning for duplicate files...")
file_hashes = {}
duplicates = []
processed_files = 0
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
try:
file_hash = get_file_hash(file_path)
if file_hash in file_hashes:
duplicates.append(file_path)
else:
file_hashes[file_hash] = file_path
processed_files += 1
except Exception as e:
print(f"Error processing file {file_path}: {e}")
print(f"Processed {processed_files} files.")
return duplicates
def move_duplicates_to_folder(duplicates, dest_folder):
"""Move duplicate files to a new folder."""
print("Moving duplicate files to duplicates folder...")
for file_path in duplicates:
filename = os.path.basename(file_path)
dest_path = os.path.join(dest_folder, filename)
if not os.path.exists(dest_path):
shutil.move(file_path, dest_path)
print(f"Moved duplicate file: {file_path} to {dest_path}")
else:
duplicate_filename = os.path.splitext(filename)[0]
duplicate_ext = os.path.splitext(filename)[1]
counter = 1
while os.path.exists(dest_path):
duplicate_filename = f"{os.path.splitext(filename)[0]}_{counter}"
filename = f"{duplicate_filename}{duplicate_ext}"
dest_path = os.path.join(dest_folder, filename)
counter += 1
shutil.move(file_path, dest_path)
print(f"Moved duplicate file: {file_path} to {dest_path}")
def run_duplicate_file_detector(folder_path):
"""Run the duplicate file detector on a specified folder."""
# Specify the destination folder for moving duplicate files
dest_folder = os.path.join(folder_path, 'duplicates')
# Create the destination folder if it doesn't exist
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
# Identify duplicate files
duplicates = identify_duplicates(folder_path)
if duplicates:
print(f"Found {len(duplicates)} duplicate files:")
for file_path in duplicates:
print(file_path)
# Move duplicate files to the destination folder
move_duplicates_to_folder(duplicates, dest_folder)
else:
print("No duplicate files found.")
if __name__ == '__main__':
# Check if a directory argument is provided
if len(sys.argv) == 2:
folder_path = sys.argv[1]
if os.path.isdir(folder_path):
run_duplicate_file_detector(folder_path)
else:
print("Error: Invalid directory path.")
else:
# Request user input for the directory path
folder_path = input("Enter the folder path to scan for duplicate files: ")
if os.path.isdir(folder_path):
run_duplicate_file_detector(folder_path)
else:
print("Error: Invalid directory path.")