【问题标题】:python pyinotify moved filespython pyinotify 移动文件
【发布时间】:2013-04-09 14:48:02
【问题描述】:

我正在使用此代码来检测何时在文件夹中创建文件/目录。在指定文件夹中创建新文件/目录时,它可以正常工作。但是,当文件/目录移动到文件夹中时,它不会通知或记录文件/目录。如何检测?

#!/usr/bin/env python

# monitors both files and dirs

import os
import pyinotify
from datetime import datetime

timestamp = datetime.today()
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE

class PTmp(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print "Created: %s " % os.path.join(event.path, event.name)
        event_log = open('/home/saad/Code/test/event_log', 'a')
        event_log.write(event.name + ' - ' + timestamp.strftime('%c') + '\n')
        event_log.close()

notifier = pyinotify.Notifier(wm, PTmp())

wdd = wm.add_watch('/home/saad/Code/test/foo', mask, rec=True)

while True:
    try:
        notifier.process_events()
        if notifier.check_events():
            notifier.read_events()
    except KeyboardInterrupt:
        notifier.stop()
        break

【问题讨论】:

    标签: python file operations pyinotify


    【解决方案1】:

    您可以观看IN_MOVED_TO 活动:

    mask = pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO
    
    class PTmp(pyinotify.ProcessEvent):
        ...
        def process_IN_MOVED_TO(self, event):
            print "Moved: %s " % os.path.join(event.path, event.name)
            event_log = open('/home/saad/Code/test/event_log', 'a')
            event_log.write(event.name + ' - ' + timestamp.strftime('%c') + '\n')
            event_log.close()
    

    (未测试,因为我现在没有可用的 Linux 机器)。

    man pagepyinotify doc 中提供了 inotify 事件的完整列表。

    【讨论】:

      【解决方案2】:

      使用 afick 工具,适用于 Linux,来自 CD 驱动程序,因此无法触摸 afick,它会通过电子邮件提醒您系统上的任何更改...如果它是网络服务器,非常建议您创建一个CD 或 DVD 并要求他们将其放入服务器中已安装的 CD DRIVE 中。否则从外部使用它,但我不知道如何实现。

      http://archive09.linux.com/feature/113944

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-07
        • 2019-01-02
        • 2013-08-20
        • 1970-01-01
        • 2018-10-25
        相关资源
        最近更新 更多