Created
July 4, 2012 23:01
-
-
Save hugosenari/3049960 to your computer and use it in GitHub Desktop.
python to run command when something changes in directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#THANKS TO: http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html | |
#time for pooling in seconds | |
TIME = 2 | |
#path to watch this version don't recursive | |
PATH = "." | |
#command to execute when something changes | |
COMMAND = "dir" | |
import os, time | |
before = dict ([(f, os.stat(f).st_mtime) for f in os.listdir (PATH)]) | |
while 1: | |
time.sleep (TIME) | |
after = dict ([(f, os.stat(f).st_mtime) for f in os.listdir (PATH)]) | |
modified = [f for f in before.keys() if not before.get(f, None) == after.get(f, None)] | |
if modified: | |
os.system(COMMAND) | |
before = after |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: make it recursive