-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_organizer.py
More file actions
53 lines (44 loc) · 1.85 KB
/
file_organizer.py
File metadata and controls
53 lines (44 loc) · 1.85 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
42
43
44
45
46
47
48
49
50
51
52
53
import os
import shutil
# The Problem: Downloads folder gets messy.
# The Solution: Organize files by extension.
def organize_directory(path):
# Dictionary mapping file extensions to folder names
extensions = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
'Documents': ['.pdf', '.docx', '.txt', '.xlsx', '.pptx'],
'Archives': ['.zip', '.rar', '.tar', '.gz'],
'Videos': ['.mp4', '.mkv', '.mov', '.avi'],
'Code': ['.py', '.js', '.html', '.css', '.cpp']
}
# Iterate over files in the directory
for filename in os.listdir(path):
file_path = os.path.join(path, filename)
# Skip directories, only move files
if os.path.isdir(file_path):
continue
# Get file extension
_, ext = os.path.splitext(filename)
# Find the category
moved = False
for folder_name, ext_list in extensions.items():
if ext.lower() in ext_list:
# Create folder if it doesn't exist
folder_path = os.path.join(path, folder_name)
os.makedirs(folder_path, exist_ok=True)
# Move the file
shutil.move(file_path, os.path.join(folder_path, filename))
print(f"Moved: {filename} -> {folder_name}/")
moved = True
break
if not moved and ext:
# Optional: Move unknown files to 'Others'
folder_path = os.path.join(path, 'Others')
os.makedirs(folder_path, exist_ok=True)
shutil.move(file_path, os.path.join(folder_path, filename))
print(f"Moved: {filename} -> Others/")
if __name__ == "__main__":
# For testing, you can change '.' to a specific path like 'C:/Users/Name/Downloads'
print("Starting organization...")
organize_directory('.')
print("Done!")