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

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

  •  28-06-2022
  •  | 
  •  

質問

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()
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top