Skip to content

Instantly share code, notes, and snippets.

@hugosenari
Created July 4, 2012 23:01
Show Gist options
  • Save hugosenari/3049960 to your computer and use it in GitHub Desktop.
Save hugosenari/3049960 to your computer and use it in GitHub Desktop.
python to run command when something changes in directory
#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
@hugosenari
Copy link
Author

TODO: make it recursive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment