-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathPyFO.py
More file actions
49 lines (42 loc) · 1.72 KB
/
PyFO.py
File metadata and controls
49 lines (42 loc) · 1.72 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
import os
class Organizer:
def __init__(self) -> None:
self.files = [f for f in os.listdir() if os.path.isfile(f)]
self.file_types = {
"Image": [".png", '.jpeg', '.jpg'],
"Video": ['.mp4', '.mkv', '.3gp', '.webp'],
"Document": ['.pdf', '.doc', '.docx', '.odf', '.odt', '.xlsx'],
"Other": []
}
def check_folders(self) -> None:
try:
for folder in self.file_types.keys():
if not os.path.exists(folder):
os.makedirs(folder)
print(f"{folder} folder created.")
except Exception as e:
print(f"Error while checking and creating folders: {e}")
def organize_files(self) -> str:
try:
for file in self.files:
for file_type, extensions in self.file_types.items():
if any(file.endswith(extension) for extension in extensions):
os.rename(file, os.path.join(file_type, file))
print(f"Moved {file} to {file_type} folder.")
remaining_files = [f for f in os.listdir() if os.path.isfile(f)]
if remaining_files:
for file in remaining_files:
os.rename(file, os.path.join("Other", file))
print(f"Moved {file} to Other folder.")
return "Organized"
except Exception as e:
print(f"Error while organizing files: {e}")
return "Not Organized completely"
def run(self) -> str:
self.check_folders()
status = self.organize_files()
return status
if __name__ == "__main__":
PyFO = Organizer()
is_complete = PyFO.run()
print(is_complete)