-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileorganize.py
More file actions
36 lines (31 loc) · 1.44 KB
/
fileorganize.py
File metadata and controls
36 lines (31 loc) · 1.44 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
import os
import shutil
def organize_files(source_folder):
# Create directories for different file types
file_types = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
'Documents': ['.txt', '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx'],
'Videos': ['.mp4', '.avi', '.mkv', '.mov', '.flv'],
'Music': ['.mp3', '.wav', '.flac', '.aac'],
'Others': [] # For all other file types
}
for dir_name in file_types.keys():
os.makedirs(os.path.join(source_folder, dir_name), exist_ok=True)
# Move files to appropriate directories
for filename in os.listdir(source_folder):
if os.path.isfile(os.path.join(source_folder, filename)):
file_ext = os.path.splitext(filename)[1].lower()
moved = False
for category, extensions in file_types.items():
if file_ext in extensions:
shutil.move(os.path.join(source_folder, filename), os.path.join(source_folder, category, filename))
moved = True
break
if not moved:
shutil.move(os.path.join(source_folder, filename), os.path.join(source_folder, 'Others', filename))
def main():
source_folder = input("Enter the path to the folder you want to organize: ")
organize_files(source_folder)
print("Files organized successfully.")
if __name__ == "__main__":
main()