forked from Grow-with-Open-Source/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-organizer.py
More file actions
41 lines (41 loc) · 2.07 KB
/
file-organizer.py
File metadata and controls
41 lines (41 loc) · 2.07 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
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import shutil
class MyHandler(FileSystemEventHandler):
def on_created(self, event):
if not event.is_directory:
file_path = event.src_path
file_name = os.path.basename(file_path)
file_extension = os.path.splitext(file_name)[1].lower()
# A dictionary takes file extensions & folder to move each formats to.
destination_mapping = {
# Key = Format/extension to handle : Value = Folder to move file format.
".zip": r"C:\Users\Precious pc\Documents\Zip files",
".png": r"C:\Users\Precious pc\Documents\png_files",
".psd": r"C:\Users\Precious pc\Documents\psd_destination",
".pdf": r"C:\Users\Precious pc\Documents\pdf_files",
}
# Default destination for unknown extensions
other_files = r"C:\Users\Precious pc\Documents\other_files"
# Get the destination directory for the file extension or use the default
destination = destination_mapping.get(file_extension, other_files)
# Move the file to the determined destination
# Check if file already exists in destination & delete it.
if os.path.exists(os.path.join(destination, file_name)):
print(f"File {file_name} already exists in the destination. Deleting it.")
os.remove(os.path.join(destination, file_name))
# Move the file to the determined destination
shutil.move(file_path, destination)
if __name__ == "__main__":
folder_to_watch = r"C:\Users\Precious pc\Documents\monitor" # Replace with the directory you want to monitor
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=folder_to_watch, recursive=False) # Set recursive to True if you want to monitor subdirectories
observer.start()
try:
while True:
pass
except KeyboardInterrupt:
observer.stop()
observer.join()