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

时间:2019-08-16 22:33:05

标签: python python-watchdog

尝试进入classes(),我认为我会制定一个现实世界的计划,以帮助我的一位同事工作。

我正在使用看门狗API来watch一个文件夹,我的行为是当文件移入该文件夹时,我想根据文件中的course_name列对其进行重命名。 csv,到目前为止简单吗?

现在,当我运行上述伪逻辑时,我不断得到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'

1 个答案:

答案 0 :(得分:2)

我发现对于同一文件,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