more os.walk() issues... probably user error

Dan Perl danperl at rogers.com
Wed Feb 16 12:07:26 EST 2005


"rbt" <rbt at athop1.ath.vt.edu> wrote in message 
news:cuvr5b$2ei$1 at solaris.cc.vt.edu...
>     def build_clean_list(self, path):
>
>         file_skip_list = ['search_results.txt']
>         dir_skip_list = ['dev', 'proc', 'Temporary Internet Files']
>
>         fs_objects = os.walk(path, topdown=True)
> ##      for fs in fs_objects:
> ##
> ##            for f in fs[2]:
> ##                if f in file_skip_list:
> ##                    print f
> ##                    fs[2].remove(f)
> ##
> ##            for d in fs[1]:
> ##                if d in dir_skip_list:
> ##                    print d
> ##                    fs[1].remove(d)
>
>         return fs_objects

Rather as an aside, the idiom for using os.walk is
    for dirpath, dirnames, dirfiles in os.walk(path):
        for f in dirnames:
            if f in file_skip_list:
                print f
                filenames.remove(f)
            if d in dir_skip_list:
                print d
                dirnames.remove(f)

More crucially for your code, returning the generator object after having 
iterated all the way through it will not do you any good.  The generator has 
an internal state that puts it at "the end of the iteration" so you cannot 
use it to iterate again. 





More information about the Python-list mailing list