2

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

#!/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
4

2 回答 2

3

您可以观看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 机器)。

手册页pyinotify 文档中提供了 inotify 事件的完整列表。

于 2013-04-09T14:50:46.550 回答
-1

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

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

于 2014-11-22T15:11:16.090 回答