-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_organizer.py
More file actions
31 lines (25 loc) · 1.05 KB
/
file_organizer.py
File metadata and controls
31 lines (25 loc) · 1.05 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
import os
import shutil
def organize_files(directory_path):
# Dictionary mapping file extensions to folder names
file_types = {
'Documents': ['.pdf', '.docx', '.txt', '.pptx'],
'Images': ['.jpg', '.jpeg', '.png', '.gif'],
'Scripts': ['.py', '.cpp', '.c', '.java'],
'Zipped': ['.zip', '.rar']
}
for filename in os.listdir(directory_path):
filepath = os.path.join(directory_path, filename)
# Skip directories
if os.path.isdir(filepath):
continue
extension = os.path.splitext(filename)[1].lower()
for folder, extensions in file_types.items():
if extension in extensions:
folder_path = os.path.join(directory_path, folder)
os.makedirs(folder_path, exist_ok=True)
shutil.move(filepath, os.path.join(folder_path, filename))
print(f"Moved {filename} to {folder}")
if __name__ == "__main__":
path = input("Enter the directory path to organize: ")
organize_files(path)