os.walk restart

Piet van Oostrum piet at vanoostrum.org
Wed Mar 31 14:50:51 CEST 2010


You have no guarantee that on the next run the directories will be
visited in the same order as in the first run (this could depend on the
filesystem). So then remembering a last directory won't do it. You could
write each completed directory name to a file, and then on the second
run check whether a directory is in that list and skip the program run
for these.

Something like this (symbolically):

lastrun = map(string.strip, logfile.readlines())
newlog = ... open logfile in append mode ...

for root, dirs, files in os.walk(basedir):
    if root not in lastrun:
        run program
        newlog.write(root)
        newlog.flush()

-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Nu Fair Trade woonwaar op http://www.zylja.com



More information about the Python-list mailing list