I have this simple script that scan an folder looking for excel files, if found then it move to an specific folder. The thing is that I want the scan to happen every 1 minute, I tought that setting time.sleep(60) in the while bucle it gonnna works, but it did not. Every time I copy an excel file to the folder that is being scanned it instantly get moved to the folder, I mean it does not wait 1 minute to rescan it is like the time.sleep it is not working.
import time
import os
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ExcelHandler(FileSystemEventHandler):
def on_created(self, event):
if event.is_directory:
return
if event.src_path.endswith('.xlsx') or event.src_path.endswith('.xls'):
# Destination folder path for moving Excel files
destination_folder = 'F:/Pedidos'
# Move the file to the destination folder
file_name = os.path.basename(event.src_path)
destination_path = os.path.join(destination_folder, file_name)
shutil.move(event.src_path, destination_path)
print(f"Moved '{file_name}' to '{destination_folder}'")
if __name__ == "__main__":
folder_to_watch = 'F:/'
event_handler = ExcelHandler()
observer = Observer()
observer.schedule(event_handler, path=folder_to_watch, recursive=False)
print(f"Watching folder '{folder_to_watch}' for Excel files...")
observer.start()
try:
while True:
print("New scan")
time.sleep(60)
print("Scanning folder")
except KeyboardInterrupt:
observer.stop()
observer.join()
I have tried increasing the time to 120 seconds and copying diferents files every 5 seconds, anyways it get moved instantly.