From d660266154543d1db5e9e34be45e2165eda3e919 Mon Sep 17 00:00:00 2001 From: Adegbemigun Adedayo <117754792+adedayoprcs@users.noreply.github.com> Date: Thu, 26 Oct 2023 17:49:55 +0100 Subject: [PATCH 1/5] Created Oberver --- File_Organizer/file-organizer.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 File_Organizer/file-organizer.py diff --git a/File_Organizer/file-organizer.py b/File_Organizer/file-organizer.py new file mode 100644 index 0000000..9848ab9 --- /dev/null +++ b/File_Organizer/file-organizer.py @@ -0,0 +1,29 @@ +import os +from watchdog.observers import Observer +from watchdog.events import FileSystemEventHandler + +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() # Convert to lowercase + + if file_extension == ".jpg": + print(f"File extension: {file_extension}") + else: + print("None") + +if __name__ == "__main__": + folder_to_watch = r"C:\Users\Precious pc\Documents\file-organizer" # 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() From 6cf60fcadffc1e7bb8bb08bb91d0a73f180466a8 Mon Sep 17 00:00:00 2001 From: Adegbemigun Adedayo <117754792+adedayoprcs@users.noreply.github.com> Date: Thu, 26 Oct 2023 18:33:20 +0100 Subject: [PATCH 2/5] destination path for files --- File_Organizer/file-organizer.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/File_Organizer/file-organizer.py b/File_Organizer/file-organizer.py index 9848ab9..b810e2d 100644 --- a/File_Organizer/file-organizer.py +++ b/File_Organizer/file-organizer.py @@ -1,18 +1,36 @@ 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() # Convert to lowercase + file_extension = os.path.splitext(file_name)[1].lower() + jpg_destination = r"C:\Users\Precious pc\Pictures\download images" + zip_destination = r"C:\Users\Precious pc\Documents\Zip files" + png_destination = r"C:\Users\Precious pc\Documents\png_files" + psd_destination = r"C:\Users\Precious pc\Documents\psd_destination" + pdf_files = r"C:\Users\Precious pc\Documents\pdf_files" + other_files = r"C:\Users\Precious pc\Documents\other_files" + if file_extension == ".jpg": - print(f"File extension: {file_extension}") + shutil.move(file_path , jpg_destination) + elif file_extension == ".zip": + shutil.move(file_path, zip_destination) + elif file_extension == ".png": + shutil.move(file_path, png_destination) + elif file_extension == ".psd": + print(file_extension) + shutil.move(file_path, psd_destination) + elif file_extension == ".pdf": + shutil.move(file_path, pdf_files) else: - print("None") + shutil.move(file_path, other_files) + if __name__ == "__main__": folder_to_watch = r"C:\Users\Precious pc\Documents\file-organizer" # Replace with the directory you want to monitor From e706f1475c1f65be839a7a11a22f6fd43a988f31 Mon Sep 17 00:00:00 2001 From: Adegbemigun Adedayo <117754792+adedayoprcs@users.noreply.github.com> Date: Sun, 29 Oct 2023 00:51:48 +0100 Subject: [PATCH 3/5] added descriptive texts --- File_Organizer/file-organizer.py | 74 +++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/File_Organizer/file-organizer.py b/File_Organizer/file-organizer.py index b810e2d..9061516 100644 --- a/File_Organizer/file-organizer.py +++ b/File_Organizer/file-organizer.py @@ -5,33 +5,44 @@ 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() - jpg_destination = r"C:\Users\Precious pc\Pictures\download images" - zip_destination = r"C:\Users\Precious pc\Documents\Zip files" - png_destination = r"C:\Users\Precious pc\Documents\png_files" - psd_destination = r"C:\Users\Precious pc\Documents\psd_destination" - pdf_files = r"C:\Users\Precious pc\Documents\pdf_files" - other_files = r"C:\Users\Precious pc\Documents\other_files" - - - if file_extension == ".jpg": - shutil.move(file_path , jpg_destination) - elif file_extension == ".zip": - shutil.move(file_path, zip_destination) - elif file_extension == ".png": - shutil.move(file_path, png_destination) - elif file_extension == ".psd": - print(file_extension) - shutil.move(file_path, psd_destination) - elif file_extension == ".pdf": - shutil.move(file_path, pdf_files) - else: - shutil.move(file_path, other_files) + try: + 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() + try: + # A dictionary takes file extensions & folder to move each formats to. + destination_mapping = { + #Key = Format/extension to handle : Value = Folder to move file format. + ".jpg": r"C:\Users\Precious pc\Pictures\download imag", + ".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", + } + except ValueError: + print("folder not found") + + # 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) + except ValueError: + print("an error occur!") + if __name__ == "__main__": folder_to_watch = r"C:\Users\Precious pc\Documents\file-organizer" # Replace with the directory you want to monitor event_handler = MyHandler() @@ -44,4 +55,15 @@ def on_created(self, event): pass except KeyboardInterrupt: observer.stop() - observer.join() + observer.join() + + + + + + + + + + + From 76a0fa3ae2e1c125849254cbf40c0e286d0ab8f2 Mon Sep 17 00:00:00 2001 From: Adegbemigun Adedayo <117754792+adedayoprcs@users.noreply.github.com> Date: Sun, 29 Oct 2023 01:10:56 +0100 Subject: [PATCH 4/5] created a README file --- File_Organizer/README.MD | 6 ++++++ File_Organizer/file-organizer.py | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 File_Organizer/README.MD diff --git a/File_Organizer/README.MD b/File_Organizer/README.MD new file mode 100644 index 0000000..1bc5d3a --- /dev/null +++ b/File_Organizer/README.MD @@ -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" \ No newline at end of file diff --git a/File_Organizer/file-organizer.py b/File_Organizer/file-organizer.py index 9061516..03e7aba 100644 --- a/File_Organizer/file-organizer.py +++ b/File_Organizer/file-organizer.py @@ -5,25 +5,23 @@ class MyHandler(FileSystemEventHandler): def on_created(self, event): - try: - if not event.is_directory: + 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() - try: + # A dictionary takes file extensions & folder to move each formats to. destination_mapping = { #Key = Format/extension to handle : Value = Folder to move file format. - ".jpg": r"C:\Users\Precious pc\Pictures\download imag", + ".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", } - except ValueError: - print("folder not found") + # Default destination for unknown extensions other_files = r"C:\Users\Precious pc\Documents\other_files" @@ -40,11 +38,9 @@ def on_created(self, event): # Move the file to the determined destination shutil.move(file_path, destination) - except ValueError: - print("an error occur!") - + if __name__ == "__main__": - folder_to_watch = r"C:\Users\Precious pc\Documents\file-organizer" # Replace with the directory you want to monitor + 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 @@ -55,7 +51,11 @@ def on_created(self, event): pass except KeyboardInterrupt: observer.stop() - observer.join() + observer.join() + + + + From c7e0207700fc50cddcd7a221cdb443ede1d2403a Mon Sep 17 00:00:00 2001 From: Adegbemigun Adedayo <117754792+adedayoprcs@users.noreply.github.com> Date: Mon, 30 Oct 2023 09:40:55 +0100 Subject: [PATCH 5/5] I removed empty new lines --- File_Organizer/file-organizer.py | 72 ++++++++++---------------------- 1 file changed, 22 insertions(+), 50 deletions(-) diff --git a/File_Organizer/file-organizer.py b/File_Organizer/file-organizer.py index 03e7aba..f11dec7 100644 --- a/File_Organizer/file-organizer.py +++ b/File_Organizer/file-organizer.py @@ -2,68 +2,40 @@ 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) - + 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() - - - - - - - - - - - - - - -