Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Created July 17, 2012 08:39
Show Gist options
  • Select an option

  • Save PyYoshi/3128066 to your computer and use it in GitHub Desktop.

Select an option

Save PyYoshi/3128066 to your computer and use it in GitHub Desktop.
ファイル監視コード
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
BASEDIR = os.path.abspath(os.path.dirname(__file__))
def getext(filename):
return os.path.splitext(filename)[-1].lower()
class ChangeHandler(FileSystemEventHandler):
def on_created(self, event):
if event.is_directory:
return
if getext(event.src_path) in ('.jpg','.png','.txt'):
print('%s has been created.' % event.src_path)
def on_modified(self, event):
if event.is_directory:
return
if getext(event.src_path) in ('.jpg','.png','.txt'):
print('%s has been modified.' % event.src_path)
def on_deleted(self, event):
if event.is_directory:
return
if getext(event.src_path) in ('.jpg','.png','.txt'):
print('%s has been deleted.' % event.src_path)
if __name__ in '__main__':
while 1:
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler,BASEDIR,recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment