python - Monitor file for an append and perform some action after the append is done -
wi've created 2 separate scripts crawl, scrape , save results csv file , upload file dropbox run manually. want monitor csv file while performing append operation , upload file once append operation over. can throw me idea or examples i'm new this. saw package named watchdog couldn't figure out how use it.
watchdog
simple use. i've written sample code here, , monitor change on *.csv
file. set path before using it... simple ./
works:
import time watchdog.observers import observer watchdog.events import patternmatchingeventhandler class changehandler(patternmatchingeventhandler): patterns = ["*.csv"] # handle csv files def __init__(self): super(changehandler, self).__init__() def process(self, event): ''' event.event_type: type of event string. event.src_path: source path of file system object triggered event event.is_directory: true if event emitted directory; false otherwise. ''' print event.event_type print event.src_path print event.is_directory # upload here def on_modified(self, event): self.process(event) def on_created(self, event): self.process(event) def on_deleted(self, event): ''' file deleted, maybe other operations? ''' pass if __name__ == '__main__': observer = observer() observer.schedule(changehandler(), path='your_csv_folder_path_here', recursive=true) observer.start() while true: time.sleep(1)
Comments
Post a Comment