我试图在每次文件更改时运行一个函数。看门狗似乎是没有民意调查的主要解决方案。
我的代码会返回错误:
Exception in thread Thread-14:
Traceback (most recent call last):
File "C:\Users\Administrator\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\Anaconda3\lib\site-packages\watchdog\observers\api.py", line 146, in run
self.queue_events(self.timeout)
File "C:\Users\Administrator\Anaconda3\lib\site-packages\watchdog\observers\read_directory_changes.py", line 77, in queue_events
winapi_events = read_events(self._handle, self.watch.is_recursive)
File "C:\Users\Administrator\Anaconda3\lib\site-packages\watchdog\observers\winapi.py", line 347, in read_events
buf, nbytes = read_directory_changes(handle, recursive)
File "C:\Users\Administrator\Anaconda3\lib\site-packages\watchdog\observers\winapi.py", line 307, in read_directory_changes
raise e
File "C:\Users\Administrator\Anaconda3\lib\site-packages\watchdog\observers\winapi.py", line 303, in read_directory_changes
ctypes.byref(nbytes), None, None)
File "C:\Users\Administrator\Anaconda3\lib\site-packages\watchdog\observers\winapi.py", line 108, in _errcheck_bool
raise ctypes.WinError()
OSError: [WinError 87] The parameter is incorrect.
代码直接来自文档并粘贴在下面:
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print("Got it!")
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='C:/Email_forwarding/Attachments/Sales/test.txt', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
我在Windows Server 2012上使用watchdog 0.8.3运行Python 3.6(Anaconda)。
编辑:尝试降级到Python 3.5.2但仍然得到相同的错误。
答案 0 :(得分:1)
将文件路径替换为父文件夹路径。
observer.schedule(event_handler, path='C:/Email_forwarding/Attachments/Sales', recursive=False)