짧은 답변 : f = open(... , 'w')
는이 FileCreatedEvent
, f.flush()
또는 f.close()
이 FileModifiedEvent
를 생성 할 수 있습니다 생성합니다. 그래서 예, 파일을 생성하면 종종 FileCreatedEvent
과 FileModifiedEvents
이 생성됩니다.
FileCreatedEvents를 안전하게 무시할 수 있는지 여부는 수행하려는 작업에 따라 다릅니다. FileCreatedEvents를 생성하지 않고 파일을 수정할 때 FileModifiedEvents를 생성 할 수 있기 때문에 파일을 만들 때마다 반응하는 데 관심이 있다면 FileCreatedEvents를 처리해야하며 아마도 FileModifiedEvents는 무시해야합니다.
일반용 감시 스크립트 (아래)를 가지고 놀아라. 그러면 모든 것이 더 분명해 져야한다.
긴 대답 : 무슨 일이 일어나고 있는지 보는 것은, 정규 감시 프로그램 straight from the docs 실행 : 이제
% mkdir ~/tmp
% cd ~/tmp
% script.py
열고 파이썬 인터프리터 :
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
을 그리고 터미널에서 w
모드의 파일 :
In [126]: f = open('/home/unutbu/tmp/foobar', 'w')
터미널 인쇄
2014-02-05 16:29:34 - <FileCreatedEvent: src_path=/home/unutbu/tmp/foobar>
당신은 어떤 경우에도보고하지 않는 파일 감시에 기록
:
In [127]: f.write('Hi')
을하지만 플러시,
In [128]: f.flush()
그것이 FileModifiedEvent를보고 할 때 :
2014-02-05 16:29:55 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>
당신은 파일에 대한 더 많은 물건을 작성하는 경우 :
In [130]: f.close()
2014-02-05 16:30:12 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>
좋아, 감사 : 더 출력이 디스크로되어 있기 때문에이 파일을 닫을 때 마찬가지로, FileModifiedEvent가보고
. 그게 내가 생각하고 있었던 것에 관한 것. 자세한 답을 고맙게 생각합니다. – dbers