[Twisted-Python] watch a directory (Win32)

Patrick Lauber digi at treepy.com
Tue Mar 8 06:18:21 MST 2005


just add something like this in your pathwather.py:

def waiting():
  try:
    old_path_contents = os.listdir (path_to_watch)
    result = win32event.WaitForSingleObject (change_handle, 500)

    #
    # If the WaitFor... returned because of a notification (as
    #  opposed to timing out or some error) then look for the
    #  changes in the directory contents.
    #
    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = os.listdir (path_to_watch)
      files_added = [f for f in new_path_contents if not f in 
old_path_contents]
      files_deleted = [f for f in old_path_contents if not f in 
new_path_contents]

      if files_added:
        print
        print time.asctime ()
        print "<Added file=\""+ files_added[0]+"\"/>"
        return "<Added file=\""+ files_added[0]+"\"/>"
      if files_deleted:
        print "<Deleted file=\""+files_deleted[0]+"\"/>"
        return "<Deleted file=\""+files_deleted[0]+"/\">"
       

      win32file.FindNextChangeNotification (change_handle)
  finally:
    pass
    #win32file.FindCloseChangeNotification (change_handle)

and call it with reactor.callLater(0,someFunctionThatCallsWaiting)   
regualary

cheers p@





More information about the Twisted-Python mailing list