-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
46 lines (27 loc) · 1.26 KB
/
Copy pathmain.py
File metadata and controls
46 lines (27 loc) · 1.26 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
import os
import shutil
class FileOrganizer:
def __init__(self, directory):
self.directory = directory
def organize_files(self):
# Creates folders for each file type and Move files to correct folders
for filename in os.listdir(self.directory):
if os.path.isfile(os.path.join(self.directory, filename)):
file_extension = os.path.splitext(filename)[1]
destination_folder = self.get_destination_folder(file_extension)
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
shutil.move(os.path.join(self.directory, filename),
os.path.join(destination_folder, filename))
def get_destination_folder(self,file_extension):
if file_extension == "":
return os.path.join(self.directory,"other")
else:
return os.path.join(self.directory, file_extension[1:].capitalize())
if __name__ == "__main__":
directory = input("Enter the directory to organize: ")
organizer = FileOrganizer(directory)
organizer.organize_files()
print("Files organized successfully.")
else:
print("Invalid directory path.")