os.walk restart

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Mar 17 18:42:35 EDT 2010


On Wed, 17 Mar 2010 15:04:14 -0700, Keir Vaughan-taylor wrote:

> I am traversing a large set of directories using
> 
> for root, dirs, files in os.walk(basedir):
>     run program
> 
> Being a huge directory set the traversal is taking days to do a
> traversal.
> Sometimes it is the case there is a crash because of a programming
> error.
> As each directory is processed the name of the directory is written to a
> file

What, a proper, honest-to-goodness core dump?

Or do you mean an exception?


> I want to be able to restart the walk from the directory where it
> crashed.
> 
> Is this possible?


Quick and dirty with no error-checking:


# Untested
last_visited = open("last_visited.txt", 'r').read()
for root, dirs, files in os.walk(last_visited or basedir):
     open("last_visited.txt", 'w').write(root)
     run program




-- 
Steven



More information about the Python-list mailing list