python watchdog monitoring directory for created files and doing something to that file

StackOverflow https://stackoverflow.com/questions/18830246

  •  28-06-2022
  •  | 
  •  

Question

I'm using watchdog to monitor a directory (and sub-directories) for created files. If the created files are .csv the program reads them (and does some small changes to the files, but this is really not part of the question). The problem is that sometimes readlines() returns an empty list and I cannot figure out why it does this. It will occationally return an empty list even if I just copy the same .csv file to a monitored directory, so it doesn't seem to be the .csv file which is the problem. I'm running python 2.7.5 on OSX. So does anyone understand why this happens?

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


class CreatedHandler(FileSystemEventHandler):

    def on_created(self, event):

        if event.is_directory:
            return
        filepath, ext = os.path.splitext(event.src_path)
        if (ext == '.csv' and '-fixed' not in filepath):
            lines = list()
            with open(event.src_path, 'r') as f:
                lines = f.readlines()
            print len(lines)

if __name__ == "__main__":
    event_handler = CreatedHandler()
    observer = Observer()
    observer.schedule(event_handler, '.', recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Was it helpful?

Solution

I think you might need to add a sleep right after the file is found. I think you script is reading it, before it has actual data in the pipe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top