os.walk restart

alex23 wuwei23 at gmail.com
Thu Mar 18 04:08:48 CET 2010


Steven D'Aprano <ste... at REMOVE.THIS.cybersource.com.au> wrote:
> # 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

Wouldn't this only walk the directory the exception occured in and not
the remaining unwalked dirs from basedir?

Something like this should work:

import os
basedir = '.'

walked = open('walked.txt','r').read().split()
unwalked = ((r,d,f) for r,d,f in os.walk(basedir) if r not in walked)

for root, dirs, files in unwalked:
  # do something
  print root
  walked.append(root)

open('walked.txt','w').write('\n'.join(walked))



More information about the Python-list mailing list