首页 > 解决方案 > Python 看门狗,在 event.modification 上观察目录并重命名文件

问题描述

试图深入classes()研究,我想我会制作一个真实世界的程序来帮助我的一位同事工作。

我正在使用看门狗 API 到watch一个文件夹,我所追求的行为是,当一个文件被移动到这个文件夹中时,我想根据course_namecsv 中的列重命名它,到目前为止很简单吗?

现在,当我运行上面的伪逻辑时,我不断得到一个FileNotFoundError但是代码确实有效 - 但 API 仍在搜索已删除/更改的文件?

从我所看到的情况来看,在我的功能之后正在执行某些东西,但我一生都无法弄清楚是什么?

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import pandas as pd
import os
from shutil import copyfile
my_path = r"<dir_to_watch>"


class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print(f'event type: {event.event_type}  path : {event.src_path}')
        df = pd.read_csv(event.src_path) # read the file
        course = df['Course Name'].unique().tolist()[0] # pass course name to a variable
        copyfile(event.src_path, f"{course}.csv") # copy file, using os.rename threw up an error.
        os.remove(event.src_path) # remove original file.
        print("file renamed")

然后我执行上面的:

if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=my_path, recursive=False)
observer.start()

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

如果需要任何其他信息,请询问。

追溯错误很长我很抱歉:

    Exception in thread Thread-8:
Traceback (most recent call last):
  File "\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "\Anaconda3\lib\site-packages\watchdog\observers\api.py", line 199, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "\Anaconda3\lib\site-packages\watchdog\observers\api.py", line 368, in dispatch_events
    handler.dispatch(event)
  File "\Anaconda3\lib\site-packages\watchdog\events.py", line 330, in dispatch
    _method_map[event_type](event)
  File "<ipython-input-7-30cb2defae10>", line 13, in on_modified
    df = pd.read_csv(event.src_path)
  File "\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1122, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1853, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 387, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 705, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'report - Copy.csv' does not exist: b'report - Copy.csv'

标签: pythonpython-watchdog

解决方案


我发现 on_modified 事件可以为同一个文件“触发”两次。我用我的应用程序的最近事件的双端队列解决了这个问题,但是 last_event 变量可以防止重复。如果您期望输入文件具有相同的名称,您可能希望找到一种重新设置 last_event 的方法。

此外,我发现在 on_modified() 中的短暂等待以允许文件被完全写入,这对于防止对该文件执行操作时的意外行为非常有用。

last_event = ''

def on_modified(self, event):
    time.sleep(1)  # wait to allow file to be fully written
    if not event.src_path == last_event:  # files we haven't seen recently
         # do something
         last_event = event.src_path

然而,一个更简单的选择,也许更 Pythonic,就是正确处理该异常:

class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
    print(f'event type: {event.event_type}  path : {event.src_path}')
    try:
        df = pd.read_csv(event.src_path) # read the file
        course = df['Course Name'].unique().tolist()[0] # pass course name to a variable
        copyfile(event.src_path, f"{course}.csv") # copy file, using os.rename threw up an error.
        os.remove(event.src_path) # remove original file.
        print("file renamed")
    except FileNotFoundError:
        # handle the error
        pass

推荐阅读