-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate_file_deleter.py
More file actions
41 lines (32 loc) · 1.31 KB
/
Copy pathduplicate_file_deleter.py
File metadata and controls
41 lines (32 loc) · 1.31 KB
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
import os
import re
def delete_duplicate_files(folder_path):
# Pattern to match " (1)", " (2)", etc. before the file extension
# Example: "filename (1).mp3"
duplicate_pattern = re.compile(r'.*\s\(\d+\)\.\w+$')
deleted_count = 0
if not os.path.exists(folder_path):
print("Error: The folder path does not exist.")
return
print(f"Scanning: {folder_path}...")
for filename in os.listdir(folder_path):
# Check if the filename matches our "copy" pattern
if duplicate_pattern.match(filename):
file_path = os.path.join(folder_path, filename)
try:
os.remove(file_path)
print(f"Deleted: {filename}")
deleted_count += 1
except Exception as e:
print(f"Error deleting {filename}: {e}")
print("-" * 30)
print(f"Process complete. Total files deleted: {deleted_count}")
if __name__ == "__main__":
# Replace this with your actual folder path
target_path = input("Enter the folder path: ").strip()
# Optional: Safety confirmation
confirm = input(f"Are you sure you want to delete copies in '{target_path}'? (y/n): ")
if confirm.lower() == 'y':
delete_duplicate_files(target_path)
else:
print("Operation cancelled.")