2016-07-12 5 views
0

주기적으로 변경되는 파일의 이름을 가져 오려고합니다. 나는이를 위해 워치 독을 사용하고있다.python Watchdog을 사용하여 파일 이름 가져 오기

import time 
from watchdog.observers import Observer 
from watchdog.events import FileSystemEventHandler 

timestr = time.strftime("%Y.%m.%d-%H.%M.%S") 

class MyHandler(FileSystemEventHandler): 
    def on_modified(self, event): 
     change_log = open('change_log_' + timestr + '.txt', 'aw') 
     change_log.write('Time the file changed: ' + timestr + '\n') 
     change_log.close() 

if __name__ == "__main__": 
    event_handler = MyHandler() 
    observer = Observer() 
    observer.schedule(event_handler, path='.', recursive=False) 
    observer.start() 

    try: 
     while True: 
      time.sleep(1) 
    except KeyboardInterrupt: 
     observer.stop() 
    observer.join() 

어떤 이유로이 명령은 "change_log"파일에 약 62 줄을 인쇄합니다. 이것은별로 유용하지 않습니다. 내가 뭘하고 싶은지 변경된 파일의 이름을 인쇄하거나 다른 모듈로 전달할 변수에 저장하는 것입니다. http://pythonhosted.org/watchdog/api.html#watchdog.events.FileSystemEvent

파일 이름을 얻기 위해 당신의 FileSystemEvent 서브 클래스 처리기 메서드에 전달되는 이벤트 객체의 src_path 속성을 사용 : 그것은 당신의 핸들러에 전송되는 이벤트 객체처럼 보이는

답변

0

은 당신이 찾는 정보가 포함되어 있습니다.

관련 문제