-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathFileOrganizer.py
43 lines (34 loc) · 1.43 KB
/
FileOrganizer.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
import os
import shutil
# Prompt the user for the directory path to organize files
path = input("Enter path: ")
# Check if the directory exists
if not os.path.exists(path):
print("Error: The specified directory does not exist.")
else:
# List all items in the specified directory
files = os.listdir(path)
# Iterate through each file in the directory
for file in files:
file_path = os.path.join(path, file)
# Skip directories
if os.path.isdir(file_path):
continue
# Split the filename and extension
filename, extension = os.path.splitext(file)
extension = extension[1:] if extension else "NoExtension" # Handle files without extensions
# Destination folder for the extension
dest_folder = os.path.join(path, extension)
# Create the directory if it does not exist
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
# Handle duplicate files by renaming them
dest_file_path = os.path.join(dest_folder, file)
counter = 1
while os.path.exists(dest_file_path):
newfilename = f"{filename}{counter}.{extension}" if extension != "NoExtension" else f"{filename}_{counter}"
dest_file_path = os.path.join(dest_folder, new_filename)
counter += 1
# Move the file
shutil.move(file_path, dest_file_path)
print(f"Moved: {file} → {dest_file_path}")