-
Notifications
You must be signed in to change notification settings - Fork 26
File organizer #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iamwatchdogs
merged 5 commits into
Grow-with-Open-Source:main
from
adedayoprcs:file-organizer
Oct 30, 2023
Merged
File organizer #15
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.