在运行此代码时,Python返回多个条目。不知道为什么它不能运行一次。
我尝试更改睡眠时间,但是不确定这里发生了什么。
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from subprocess import call
class Watcher:
DIRECTORY_TO_WATCH = r"C:\Users\tgaluska\File"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print ("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
def on_any_event(self,event):
if event.is_directory:
return None
elif event.event_type == 'modified':
print ("Received modified event - %s." % event.src_path)
if __name__ == '__main__':
w = Watcher()
w.run()
Received modified event - C:\Users\tg\File\Superstore.xls.
Received modified event - C:\Users\tg\File\Superstore.xls.
Received modified event - C:\Users\tg\File\Superstore.xls.
Received modified event - C:\Users\tg\File\Superstore.xls.
Received modified event - C:\Users\tg\File\Superstore.xls.
Received modified event - C:\Users\tg\File\Superstore.xls.
在线
收到的已修改事件-C:\ Users \ tg \ File \ Superstore.xls。