I want to search in Folder for a file and when it detects the file he look for a string in the filename. The file is then to be moved in such a way which string was discovered.
e.g.:
I have a file named "blablie rel blub.pdf" And I am searching for either rel or musik. If its rel it should go in the Religion folder and if its musik it should go in the Musik folder.
Thats what I have:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import fnmatch
import os
import json
import time
class MyHandler(FileSystemEventHandler):
i = 1
def on_modified(self, event):
for file in os.listdir('.'):
if fnmatch.filter(file, '[rel]'):
for filename in os.listdir(folder_to_track):
src = folder_to_track + "/" + filename
new_destination = folder_destination + "/Religion/" + filename
os.rename(src, new_destination)
elif fnmatch.filter(file, '[musik]'):
for filename in os.listdir(folder_to_track):
src = folder_to_track + "/" + filename
new_destination = folder_destination + "/Musik/" + filename
os.rename(src, new_destination)
folder_to_track = "/Users/Nils/Downloads/oldfold"
folder_destination = "/Users/Nils/Downloads/newfold"
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
observer.stop()
observer.join()