0

I'm looking for a way to automatically detect a newly uploaded file on a ftp server. Until now, I was working with watchdog, as I only needed to monitor a local folder. Is there a way to use a similar structure to monitor a ftp server?

import time 
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class MyHandler(FileSystemEventHandler):
    def wait_till_file_is_created(self, source_path):
        historicalSize = -1
        while (historicalSize != os.path.getsize(source_path)):
            historicalSize = os.path.getsize(source_path)
            time.sleep(1)

    def on_created(self, event): # when file is created
        self.wait_till_file_is_created(event.src_path) 
        
        #followed by calculations on the imported data


observer = Observer()
event_handler = MyHandler() # create event handler
observer.schedule(event_handler, path = folder_path, recursive=False)

observer.start()
try:
    while True:
        time.sleep(10)
except KeyboardInterrupt:
    observer.stop()
    print("Observer Stopped")

observer.join()
Data123
  • 5
  • 4

0 Answers0