Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions File_Organizer/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
A file organizer is a software application designed to streamline the management of your files. It achieves this by actively monitoring a designated source path, typically a folder, in a continuous loop. When a file emerges in this source location, the program promptly relocates it to a specified destination path based on the file's extension.

Moreover, if the file being moved to the destination folder already exists there, the program takes care of the redundancy issue by automatically deleting the redundant file.

NOTE: if testing with local computer, makes sure to have to have "r" before the paths specified.
E.g r"C:\Users\Precious pc\Downloads"
41 changes: 41 additions & 0 deletions File_Organizer/file-organizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()