Python看门狗,监视目录并在event.modification上重命名文件 [英] Python watchdog, watch a directory and rename file on event.modification
问题描述
尝试进入classes()
,我认为我会制定一个现实世界的计划,以帮助我的一位同事工作.
attempting to dive into classes()
and I thought I'd make a real-world program that would help one of my colleagues at work.
我正在使用看门狗API来watch
文件夹,我的行为是,当文件移入该文件夹时,我想根据csv中的course_name
列对其进行重命名,很简单最右边?
I'm using the watchdog API to watch
a folder, the behavior I'm after is that when a file is moved into this folder I want to rename it according to course_name
column in the csv, simple so far right?
现在,当我运行上述伪逻辑时,我一直得到FileNotFoundError
,但是代码可以正常工作-但API仍在搜索已删除/更改的文件吗?
now when I run the above pseudo logic I keep getting a FileNotFoundError
however the code does work - but the API is still searching for the file that was removed/changed?
从我看到的东西可以看出,我的功能正在执行,但是我一生都无法发现是什么?
from what I can see something is executing after my function but I can't for the life of me figure out what?
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()
如果需要任何其他信息,请询问.
If any additional information is needed please ask.
回溯错误很长,我很抱歉:
the traceback error is quite long my apologies :
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事件可以对同一文件触发"两次. 我为我的应用程序使用了一系列近期事件解决了这个问题,但是last_event变量可以防止重复.如果期望输入文件具有相同的名称,则可能需要找到一种重新设置last_event的方法.
I have found that on_modified events can 'fire' twice for the same file. I solved this with a deque of recent events for my application, but a last_event variable would work to prevent duplicates. You may want to find a way of re-setting last_event, if you are expecting input files with the same name.
我还发现on_modified()中有短暂的等待时间,以允许完全写入文件对于在对该文件执行操作时防止意外行为非常有用.
Also I have found a short wait in on_modified() to allow the file to be fully written to be very useful in preventing unexpected behavior if performing operations on that file.
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,将只是正确地处理该异常:
An easier option however, and perhaps more pythonic, would be just to handle that exception properly:
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
这篇关于Python看门狗,监视目录并在event.modification上重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!