dir을 모니터링하고 dir에 하위 디렉토리가 있고 subdir에 .md
인 somes 파일이 있습니다. 나는이 문서를 읽고,Python pyinotify를 사용하여 특정 접미사 파일을 모니터 할 수 있습니다.
나는 단지 .md 파일을 모니터링 할 (아마도 ... 등 * .swp 같은 다른 파일이 있습니다), 거기 만 ExcludeFilter
이며, 문제의 : https://github.com/seb-m/pyinotify/issues/31 말하지만, dir 만 필터가 될 수 있지만 파일은 아닙니다.
이제 필터를 process_*
에 입력하고 event.name
을 확인하여 fnmatch
으로 확인하십시오.
그래서 지정된 접미사 파일 만 모니터링하려는 경우 더 좋은 방법이 있습니까? 감사.
이 내가 쓴 주요 코드 :
!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyinotify
import fnmatch
def suffix_filter(fn):
suffixes = ["*.md", "*.markdown"]
for suffix in suffixes:
if fnmatch.fnmatch(fn, suffix):
return False
return True
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
if not suffix_filter(event.name):
print "Creating:", event.pathname
def process_IN_DELETE(self, event):
if not suffix_filter(event.name):
print "Removing:", event.pathname
def process_IN_MODIFY(self, event):
if not suffix_filter(event.name):
print "Modifing:", event.pathname
def process_default(self, event):
print "Default:", event.pathname
나는 이전에 내가 이것을 작동시키지 못한다고 말했지만 이제는 얻었습니다. 'pevent' 호출 가능 변수가'Event'를 매개 변수로 취하기 때문에'SUFFIXES'을 편집하고'*'(그렇지 않으면 결코 일치하지 않습니다)를 제거하고'fn.name'에서'splitext'를 호출해야합니다. +1, 내 솔루션보다 좋네, 그리고 실제로는 'pevent'로 해결하려고했지만 어떤 이유로 작동하지 못했습니다. –
대단히 고맙습니다. 이쪽은 정말 멋집니다! 코드에 약간의 오류가 있습니다. suffix_filter를'pevent'에 대한 콜백으로 사용할 때 suffix_filter의 인수는'event'이므로'os.path.splitext (fn.name) [1]'이어야합니다 –
Tanky, @Paulo : 가리키는 것에 감사드립니다 코딩 오류를 밖으로. 가능할 때마다 테스트되지 않은 항목은 게시하지 않지만 내 OS에는 inotify가 없기 때문에이 경우에는 실행할 수 없습니다. – martineau