获取已修改文件的名称 python

Grab the name of a modified file python

在脚本中有一个观察者算法,我从这里改编而来: https://www.michaelcho.me/article/using-pythons-watchdog-to-monitor-changes-to-a-directory

我现在的目标是添加几行以获取任何已修改文件的名称,这样我就可以使用 if 语句检查某个文件,例如:

if [modified file name] == "my_file":
    print("do something")

我没有任何看门狗或文件监视的经验,所以我正在努力寻找这个问题的答案。我如何接收修改后的文件名?

看门狗 class 的当前设置非常无用,因为它只是打印...它没有 return 任何东西。

让我提供一个不同的方法:

以下将为您提供过去 12 小时内修改过的文件列表:

result = [os.path.join(root,f) for root, subfolder, files in os.walk(my_dir) 
            for f in files 
            if dt.datetime.fromtimestamp(os.path.getmtime(os.path.join(root,f))) > 
            dt.datetime.now() - dt.timedelta(hours=12)]

您可以保存 last_search_time 并在后续搜索中使用它,而不是固定的增量时间。

然后您可以搜索结果以查看它是否包含您的文件:

if my_file in result:
    print("Sky is falling. Take cover.")