我如何修复TypeError:dispatch()缺少1个必需的位置参数:'event'

问题描述 投票:-2回答:1

我正在尝试创建一个自动对文件进行排序的脚本。当然,我需要能够移动文件才能使整个工作正常进行,但是我什至不能这样做。

我总是收到相同的错误消息,如果我正确的话,它似乎不是来自我的代码。我附上了我的代码,如果能帮助我,将不胜感激。我知道我不应该发布整个文件,但实际上我不知道此错误来自何处。

这是我的代码:

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

event = None

class MyHandler(FileSystemEventHandler):
    i = 1
    def on_modified(self, event):
        for filename in os.listdir(folder_to_track):
            src = folder_to_track + "/" + filename
            new_destination = folder_destination + "/" + filename
            os.rename(src, new_destination)

folder_to_track = '/Users/Balduin/Pictures/Florens1019'
folder_destination = '/Users/Balduin/Pictures/Florens1019/raw-jpeg'
event_handler = MyHandler
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()

try:
    while True:
        time.sleep(10)
except KeyboardInterrupt:
    observer.stop()
observer.join

这是我得到的错误:

Exception in thread Thread-6:
Traceback (most recent call last):
  File "C:\Users\Balduin\Anaconda3\lib\threading.py", line 917, in 
_bootstrap_inner
    self.run()
  File "C:\Users\Balduin\Anaconda3\lib\site- 
packages\watchdog\observers\api.py", line 199, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "C:\Users\Balduin\Anaconda3\lib\site- 
packages\watchdog\observers\api.py", line 368, in dispatch_events
    handler.dispatch(event)
TypeError: dispatch() missing 1 required positional argument: 'event'
python python-3.x python-watchdog
1个回答
0
投票

尝试

event_handler = MyHandler()

而不是

event_handler = MyHandler

dispatch()watchdog文件(api.py)中定义的[\Anaconda3\lib\site-packages\watchdog\observers\api.py函数需要一个参数(event):

handler.dispatch(event)

但是,您尚未实例化您的event_handler,因此在调用handler.dispath()时不会传递任何参数。

© www.soinside.com 2019 - 2024. All rights reserved.