简体   繁体   English

为什么此看门狗脚本在我的主文件夹中不起作用?

[英]Why doesn't this watchdog script work in my home folder?

I am writing a python script for a watching changes in a certain folder: 我正在编写一个python脚本来监视某个文件夹中的更改:

#!/usr/bin/env python3

import time
import os
import subprocess

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.txt", "*.html"]
    # targetDir = "/Users/kaiyin/Movies/screencasts_small/"

    def process(self, event):
        # p = subprocess.Popen(["/usr/local/bin/CocoaDialog", "bubble", "--title", "Watch folder", "--text", "New file" + event.src_path],
        #                  stdout=subprocess.PIPE,
        #                  stderr=subprocess.PIPE)
        # out, err = p.communicate()
        print("Something happened...")

    def on_created(self, event):
        print("Triggered...")
        self.process(event)
    def on_modified(self, event):
        print("Triggered...")
        self.process(event)

if __name__ == "__main__":
    arg = "/Users/kaiyin/Movies/screencasts"
    os.system("ls " + arg)
    observer = Observer()
    observer.schedule(MyHandler(), path=arg if arg else ".")
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()


# import os
# mystat = os.stat("/Users/kaiyin/Movies/screencasts")
# mystat[0]
# oct(mystat[0])
# int(oct(mystat[0]), 8)

The problem is that creation and modification of files does not trigger the process function at all, but if I change arg = "/tmp/somefolder" , then it totally works. 问题在于创建和修改文件根本不会触发流程功能,但是如果我更改arg = "/tmp/somefolder" ,那么它完全可以工作。 I am very confused now. 我现在很困惑。

Permissons of the folder: 文件夹的许可证:

drwxr-xr-x  7 kaiyin  staff   238B Aug  7 17:03 /Users/kaiyin/Movies/screencasts/

I don't know this watchdog module, but you check the directory with ohter method 我不知道该watchdog模块,但是您可以使用其他方法检查目录

while True:
    now = datetime.datetime.now()
    ago = now - datetime.timedelta(seconds=60)

    file_list = []
    for path in glob.glob(FILES + "/*.avi"):
        (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(path)

        if datetime.datetime.fromtimestamp(mtime) > ago:
            file_list.append(path)

    if(file_list): 
        ## process yuor list
        process(file_list)

    time.sleep(30)

This code check the modification time of file and put it in the file_list . 此代码检查文件的修改时间,并将其放入file_list You can extend this verification and put information in a dictionary for detect changes in files. 您可以扩展此验证,并将信息放入dictionary以检测文件中的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM