faqts : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks : File Handling

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

6 of 11 people (55%) answered Yes
Recently 1 of 5 people (20%) answered Yes

Entry

What is the best and fastest way to scan a whole list of foldersto see if something has been added or not?

Jun 27th, 2000 01:30
Aaron Digulla, Nathan Wallace, Darrell


If your talking about NT then give this a try.

    http://www.dorb.com/darrell/win32WorkSvr/makeThumbsDG.py

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)

On Unix, things are a bit more complicated (there is no way to get
notified when a directory is changed). Instead, you have to check
for the modification time (mtime). The mtime of a directory will
change if an item in that directory is added, deleted or renamed.
It will *not* change when an item is modified (for example, if a file
is overwritten or an item is added to a subdirectory).

You will have to use os.path.walk() to recusively scan the directory
tree if you don't have a list of folders to check. Since that can
take a long time, you should use a cache which contains the
names of all directories plus their mtimes (get them with
os.stat (path)[stat.ST_MTIME]). Next time, you should load
that chache and just check and re-read directories which have
changed.