faqts : Computers : Programming : Languages : Python : Snippets : Stream Manipulation : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 292 people (6%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Scanning folders for new files

Jun 19th, 2003 02:15
Frank van Wijgerden, Nathan Wallace, unknown unknown, Hans Nowak, Snippet 386, Darrell


"""
Packages: operating_systems.windows
"""
"""
> what is the best and fastest way to scan a whole list of folders
> to see if something is added or not. Please note that this should
> also work for more than 1000 folders (and it shouldn't slow down
> the system too much).
If your talking about NT then give this a try.
http://www.dorb.com/darrell/win32WorkSvr/makeThumbsDG.py
There is also an ASPN Python Cookbook recipe at:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/156178
"""
import win32file, win32event, os, winnt, traceback
def main():
    """
    Use events to avoid polling.
    A sleep is used to give most files a chance to finish
    being writen. Sort of a hack and might not be needed.
    The notification event LAST_WRITE seems to trigger
    at the start and end of writing. So a single file
    will trigger this twice.
    """
    hnd=win32file.FindFirstChangeNotification\
        (os.path.abspath('.')+os.sep+'incoming'\
        ,0, winnt.FILE_NOTIFY_CHANGE_LAST_WRITE )
    while 1:
        int = win32event.WaitForSingleObject( hnd, -1)
            # Try to give the file time to finish writing
        time.sleep(2)
        print 'run'
        try:
            passed, failed, skipped = makeThumbNails()
        except:
            if '-d' in sys.argv:
                traceback.print_exc()
            raise ThumbNailException
        win32file.FindNextChangeNotification(hnd)
main()