CODE‎ > ‎Python‎ > ‎

監控目錄內檔案

import watcher
w = watcher.Watcher(dir, callback)
w.flags = watcher.FILE_NOTIFY_CHANGE_FILE_NAME
w.start()
參考官網
import os, time
path_to_watch = "c:\temp"
before = dict ([(f, None) for f in os.listdir (path_to_watch)]) 
while 1: 
  time.sleep (10) 
  after = dict ([(f, None) for f in os.listdir (path_to_watch)]) 
  added = [f for f in after if not f in before] 
  removed = [f for f in before if not f in after] 
  if added: print "Added: ", ", .join (added) 
  if removed: print "Removed: ", ", ".join (removed) 
  before = after
Comments