简体   繁体   English

无法将正则表达式与pyinotify一起使用

[英]Unable to use regex with pyinotify

It's possible to use a regex with inotify shell command, but not with pyinotify. 可以将正则表达式与inotify shell命令一起使用,但不能与pyinotify一起使用。 I could get the list of directories using the regex and pass it to add_watch, but, the folders "Do*" are dynamic, in the sense that they are created and destroyed very often and hence, creating a rigid list and passing it to the add_watch function would be inaccurate. 我可以使用正则表达式获取目录列表,并将其传递给add_watch,但是文件夹“ Do *”是动态的,从某种意义上说,它们经常创建和销毁,因此创建了一个刚性列表并将其传递给add_watch函数将不准确。

I've tried to compile the regex and pass it to add_watch and it doesn't work, probably because it expects a string or list of strings. 我试图编译正则表达式并将其传递给add_watch,但它不起作用,可能是因为它需要一个字符串或字符串列表。

import pyinotify,subprocess,re
def onChange(ev):
  subprocess.run("echo 'changed'", shell = True)

wm = pyinotify.WatchManager()
regex_dir = re.compile('/var/run/shm/Do*/updates/ab*.xml')
wm.add_watch(regex_dir, pyinotify.IN_CLOSE_WRITE , onChange)
notifier = pyinotify.Notifier(wm)
notifier.loop()

I would like to pass a regex to add_watch function of pyinotify without having to create a rigid list and then pass that since the directory contents would vary. 我想将正则表达式传递给pyinotify的add_watch函数,而不必创建严格的列表,然后将其传递,因为目录内容会有所不同。

WatchManager.add_watch accepts a do_glob option that allows to perform globbing on pathname. WatchManager.add_watch接受do_glob选项 ,该选项允许对路径名执行do_glob

You pass a unicode str and not a regex object for the path argument. 您传递一个unistr而不是path参数的正则表达式对象。

dir_glob = '/var/run/shm/Do*/updates/ab*.xml'
wm.add_watch(dir_glob, pyinotify.IN_CLOSE_WRITE, onChange, do_glob=True)

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

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