2016-07-12 19 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

は、あなたが探している情報が含まれています。

関連する問題