0

I currently have 2 ways of looking at a folder and checking to see if there is a new file in it. I have used watchdog and os. Below are the code snippets of what I have used.

Watchdog:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d - %H:%M:%S')
    path2 = 'G:\\Actuarial\\Pricing\\IBOTT\\Team\\NP\\Projects\\Claims Automation\\Processed Files'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path2, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()       

OS:

for filename in os.listdir('Processed Files'):
    if filename == None or filename == '.ipynb_checkpoints':
        pass
    else:
        print('File: ' + str(filename))

I'm trying to implement a program that is able to take the excel file that has been input into a folder, if that be in a zip format or a normal excel format and process it and then move it.

Watchdog is able to display any new files that are being moved into the folder, however, I'm not sure how I can use this to actually process the file? Is there a way to get the path or the file that the observer sees and read that instead?

Nhyi
  • 373
  • 1
  • 12
  • 1
    What exactly is the issue with the existing code? – Karl Knechtel Nov 01 '21 at 12:44
  • `watchdog` will be able to be more efficient than polling using `os.listdir()`. – AKX Nov 01 '21 at 12:44
  • I get from watchdog that it displays the file, but how can I get it to move the file as well as use that file that it found to read into a dataframe? Is there a way to select the file that watchdog outputs? – Nhyi Nov 01 '21 at 12:46
  • Please [edit] your question and add what it is you want to know. – martineau Nov 01 '21 at 12:50
  • Off-hand, it looks like you need to write your own event-handler and schedule that. – martineau Nov 01 '21 at 12:53
  • So I've changed the question format, when I drop a new file into a folder, watchdog prints a message to the console, however, how can the actual file name that it recognises into the program as a string for example? – Nhyi Nov 02 '21 at 10:41

0 Answers0