Skip to content
Open
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
2 changes: 1 addition & 1 deletion Binary-Gene-Classifier-Model/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
numpy==1.25.0
pandas==2.0.1
scipy==1.11.0
scikit-learn==1.3.0
scikit-learn>=1.5.0
datasets==2.16.0
86 changes: 56 additions & 30 deletions File_Organizer/file-organizer.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,67 @@
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from pathlib import Path
import shutil
class MyHandler(FileSystemEventHandler):
def on_created(self, event):
import time

from watchdog.observers import Observer
from watchdog.events import FileSystemEvent, FileSystemEventHandler

CURRENT_WORKING_DIR: Path = Path.cwd()
FILE_TYPES: dict[str, list[str]] = {
"images": [".jpg", ".jpeg", ".png", ".gif", ".webp"],
"videos": [".mp4", ".mkv", ".avi", ".mov"],
"audio": [".mp3", ".wav", ".flac", ".m4a"],
"documents": [".pdf", ".docx", ".txt"],
"archives": [".zip", ".rar", ".7z"],
}
DESTINATION_MAPPING: dict[str, Path] = {
"images": CURRENT_WORKING_DIR / "Images",
"videos": CURRENT_WORKING_DIR / "Videos",
"audio": CURRENT_WORKING_DIR / "Audio Files",
"documents": CURRENT_WORKING_DIR / "Documents",
"archives": CURRENT_WORKING_DIR / "Archives",
"others": CURRENT_WORKING_DIR / "Others"
}

class FileEventHandler(FileSystemEventHandler):
def on_created(self, event: FileSystemEvent) -> None:
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: Path = Path(event.src_path)
file_name: str = file_path.name
file_extension: str = file_path.suffix.lower()
category: str = ""

for category_name, extensions in FILE_TYPES.items():
if file_extension in extensions:
category = category_name
break
else:
category = "others"

destination: Path = DESTINATION_MAPPING[category]

if destination.joinpath(file_name).exists():
print(f"File {file_name} already exists in the destination. Renaming it")
count = 1
while True:
if destination.joinpath(f"{file_path.stem}_{count}{file_extension}").exists():
count += 1
else:
destination_path = destination / f"{file_path.stem}_{count}{file_extension}"
break
else:
destination_path = destination / file_name

shutil.move(file_path, destination_path)

if __name__ == "__main__":
folder_to_watch = r"C:\Users\Precious pc\Documents\monitor" # Replace with the directory you want to monitor
event_handler = MyHandler()
folder_to_watch: Path = CURRENT_WORKING_DIR / "monitor"
event_handler = FileEventHandler()
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
time.sleep(60)
except KeyboardInterrupt:
observer.stop()
observer.join()
Loading