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

Dan Perl danperl at rogers.com
Wed Feb 16 11:55:46 EST 2005


"rbt" <rbt at athop1.ath.vt.edu> wrote in message 
news:cuvsmi$4bb$1 at solaris.cc.vt.edu...
> rbt wrote:
>> This function is intended to remove unwanted files and dirs from 
>> os.walk(). It will return correctly *IF* I leave the 'for fs in 
>> fs_objects' statement out (basically leave out the entire purpose of the 
>> function).
>>
>> It's odd, when the program goes into that statment... even when only a 
>> 'pass', and nothing else is present, nothing is returned. Why is that? 
>> I'm testing Python 2.4 on Linux x86 and WinXP. Results are the same on 
>> either platform.
>>
>>     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
>>
>>
>
> Just to clarify, it's wrong of me to say that 'nothing is returned'... in 
> either case, this is what is returned:
>
> Here's what was returned and its type:
> ----------------------------------------
> <generator object at 0x407dbe4c>
> <type 'generator'>
> ----------------------------------------
>
> But, I can't iterate over the returned object when I descend into the for 
> statement I mentioned above.

What do you mean by not being able to iterate over the returned object? 
What kind of error are you getting?  Have you tried to debug the code?

BTW, os.walk indeed returns a generator.  You should familiarize yourself 
with generators and iterators if you haven't done so yet. 





More information about the Python-list mailing list