Skip to content

Commit 030df28

Browse files
committed
Added a python script to organize files to corresponding directories
1 parent 7b47319 commit 030df28

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

File Organizer/file_organizer.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import shutil
3+
4+
# Define the directory to organize
5+
root_dir = input("Enter Path Directory: ")
6+
7+
# Define the file types and their corresponding subdirectories
8+
file_types = {
9+
'images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
10+
'documents': ['.pdf', '.doc', '.docx', '.txt', '.xlsx', 'csv'],
11+
'videos': ['.mp4', '.mkv', '.avi', '.mov'],
12+
'audio': ['.mp3', '.wav', '.ogg'],
13+
'archives': ['.zip', '.rar', '.7z', '.tar'],
14+
'others': []
15+
}
16+
17+
def organize_files(root_dir):
18+
# Create subdirectories if they don't exist
19+
for subdirectory in file_types.keys():
20+
subdirectory_path = os.path.join(root_dir, subdirectory)
21+
if not os.path.exists(subdirectory_path):
22+
os.makedirs(subdirectory_path)
23+
24+
# Iterate through all files in the root directory
25+
for filename in os.listdir(root_dir):
26+
file_path = os.path.join(root_dir, filename)
27+
28+
# Skip if it's a directory
29+
if os.path.isdir(file_path):
30+
continue
31+
32+
# Get the file extension
33+
file_extension = os.path.splitext(file_path)[1].lower()
34+
35+
# Determine the subdirectory based on the file extension
36+
for subdirectory, extensions in file_types.items():
37+
if file_extension in extensions:
38+
subdirectory_path = os.path.join(root_dir, subdirectory)
39+
shutil.move(file_path, subdirectory_path)
40+
print(f"Moved '{filename}' to '{subdirectory}'")
41+
break
42+
else:
43+
# If the file type is not recognized, move it to 'others'
44+
subdirectory_path = os.path.join(root_dir, 'others')
45+
shutil.move(file_path, subdirectory_path)
46+
print(f"Moved '{filename}' to 'others'")
47+
48+
if __name__ == "__main__":
49+
organize_files(root_dir)

0 commit comments

Comments
 (0)