-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
81 lines (65 loc) · 2.85 KB
/
main.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import sys
import time
import logging
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Define the directory to monitor
source_dir = "/Users/tanishjain/Downloads"
destinations = {
"audio": "/Users/tanishjain/Desktop/Adi/Audio",
"images": "/Users/tanishjain/Desktop/Adi/Images",
"video": "/Users/tanishjain/Desktop/Adi/Video",
"packages": "/Users/tanishjain/Desktop/Adi/Packages",
"csv": "/Users/tanishjain/Desktop/Adi/CSV",
"dmg": "/Users/tanishjain/Desktop/Adi/DMG",
"doc": "/Users/tanishjain/Desktop/Adi/DOC",
}
# Function to create directories if they don't exist
def create_directories():
for path in destinations.values():
if not os.path.exists(path):
os.makedirs(path)
# Define a custom event handler that will respond to file system events
class FileEventHandler(FileSystemEventHandler):
def on_modified(self, event):
create_directories() # Ensure directories are created
for entry in os.scandir(source_dir):
if entry.is_file():
name = entry.name
extension = name.split(".")[-1].lower()
for category, destination in destinations.items():
if extension in {"wav", "mp3"} and category == "audio":
shutil.move(entry.path, destination)
elif extension in {"jpg", "jpeg", "png"} and category == "images":
shutil.move(entry.path, destination)
elif extension in {"mov", "mp4"} and category == "video":
shutil.move(entry.path, destination)
elif extension == "csv" and category == "csv":
shutil.move(entry.path, destination)
elif extension == "pkg" and category == "packages":
shutil.move(entry.path, destination)
elif extension == "dmg" and category == "dmg":
shutil.move(entry.path, destination)
elif extension in {"pdf", "doc", "docx", "eps"} and category == "doc":
shutil.move(entry.path, destination)
# Create an observer and attach the event handler
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = FileEventHandler()
observer = Observer()
observer.schedule(event_handler, path=source_dir, recursive=False)
# Start the observer
observer.start()
try:
while True:
# The observer runs in the background, so you need to keep the main thread running
time.sleep(1)
except KeyboardInterrupt:
# Gracefully stop the observer when you press Ctrl+C
observer.stop()
# Wait for the observer to complete
observer.join()