Python 看门狗,监视目录并在 event.modification 上重命名文件

Python watchdog, watch a directory and rename file on event.modification

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

我正在使用看门狗 API 到 watch 一个文件夹,我想要的行为是当一个文件被移动到这个文件夹时我想根据 course_name csv 中的列,到目前为止简单吗?

现在,当我 运行 上述伪逻辑时,我不断得到一个 FileNotFoundError 但是代码 确实 工作 - 但 API 是仍在搜索 removed/changed?

的文件

据我所知,我的函数后正在执行某些操作,但我终究无法弄清楚是什么?

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'

我发现 on_modified 事件可以对同一个文件 'fire' 两次。 我用我的应用程序的最近事件的双端队列解决了这个问题,但是 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