如何监视添加到python监视程序中的路径列表的新路径

时间:2017-08-23 10:01:55

标签: python python-2.7

python watchdog monitoring file for changes

我已经引用了上面的链接,并且能够监视目录中的文件更改。路径将从csv文件中填充。

现在我想运行程序,当它运行时,我想监视添加到csv文件的新路径,而不会中断或停止程序执行

#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import logging
import csv


class TestHandler(FileSystemEventHandler):

    def on_created(self, event):
        print("created: " + event.src_path)

    def on_deleted(self, event):
        print("deleted: " + event.src_path)

    def on_modified(self, event):
        print("modified: " + event.src_path)
    def on_moved(self, event):
        print("moved/renamed: " + event.src_path)
if __name__ == "__main__":

    paths=[]
    with open('F:\August Project Dev\Log Monitor\LogPath.csv', 'rb') as f:
        reader = csv.DictReader(f)
        for row in reader:
            paths.append(row['LogPath'])

    event_handler = TestHandler()
    observer = Observer()

    observers = []

    # iterate through paths and attach observers
    for line in paths:
         # convert line into string and strip newline character
        targetPath = str(line).rstrip()

        # Schedules watching of a given path
        observer.schedule(event_handler, targetPath)
        # Add observable to list of observers
        observers.append(observer)

    # start observer
    observer.start()

    try:
        while True:
            # poll every 5 second
            print 'while loop'
            time.sleep(5)
    except KeyboardInterrupt:
        for o in observers:
            o.unschedule_all()
            # stop observer if interrupted
            o.stop()
    for o in observers:
        # Wait until the thread terminates before exit
        o.join()

0 个答案:

没有答案