我有一个使用watchdog python模块在Windows上运行的python脚本,该模块旨在递归检测其中包含文件夹的文件夹中的文件位置更改。当我将文件移动到当前位置之外的内部文件夹中时,将发生FileDeleted事件,而不是FileMoved事件。我想知道文件的结束位置是什么,我可以在Mac上执行,但不能在Windows上执行。我读过this article,这表明问题出在Windows。
这是我的代码:
class Handler(PatternMatchingEventHandler):
@staticmethod
if event.event_type == 'created' or event.event_type == 'modified':
# do something if file created.
elif event.event_type == 'deleted'
# do something if file deleted
elif event.event_type == 'moved':
# do something if file moved
与文件创建和删除事件相反,如何使文件移动事件发生?还是有一种变通办法,使我可以通过获取最终文件路径将DeletedEvent像MovedEvent一样对待?
任何帮助将不胜感激!
答案 0 :(得分:1)
尝试像这样分别定义每个事件:
def on_created(event):
print(f"hey, {event.src_path} has been created!")
def on_deleted(event):
print(f"Someone deleted {event.src_path}!")
def on_modified(event):
print(f"{event.src_path} has been modified")
def on_moved(event):
print(f"someone moved {event.src_path} to {event.dest_path}")
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved
无论如何,我认为在此链接中您会找到所需的信息:https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/