Python Watchdog - 如何仅在创建文件时查看?

标签 python python-watchdog

来自Watchdog Quickstart ,我可以看到所有事件,例如修改文件夹、删除文件、创建文件等。但是,我只想查看文件何时创建。我必须更改什么才能只看到创建的文件?

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

最佳答案

您可能想看一下 watchdog.events LoggingEventHandler。除了启动 LoggingEventHandler 模块外,您附加的代码中没有任何内容与跟踪特定文件系统操作有关,我可以假设它处理所有文件系统事件?

关于Python Watchdog - 如何仅在创建文件时查看?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54451542/

相关文章:

python - 替换 numpy 数组中包含 NaN 的值

python - 我需要更新特定用户 Django 的图像

python - 在Python中将大写字符串转换为句子大小写

python - 如何在单击按钮后禁用按钮并在登录过程完成后通过检查 django 中的服务器响应来启用它

python - 使 .log 文件持久化

python - watchdog(python) - 仅监视一种文件格式并忽略 'PatternMatchingEventHandler' 中的其他所有内容

python - 获取文件复制到文件夹的时间(Python)

python - 如何解决Python 2中无法读取数组的问题

python - 处理 Watchdog 的 OSError

python - 如何将参数传递给我的 Python watchdog.events.PatternMatchingEventHandler