[Tutor] 2.7.3 generator objects

Ray Jones crawlzone at gmail.com
Sun Sep 2 08:55:15 CEST 2012


On 09/01/2012 11:39 PM, Alan Gauld wrote:
> On 02/09/12 06:44, Ray Jones wrote:
>> I was playing with os.walk today. I can use os.walk in a for loop (does
>> that make it an iterator or just an irritable? ^_^), but if I assign
>> os.walk to 'test' (test = os.walk(<path>)), that variable becomes a
>> generator object that does not work in a for loop.
>
>
> It does for me....
>
> >>> home = os.walk('/home/alang/src/Python')
> >>> for root,dirs,files in home:
> ...     print root
> ...
> /home/alang/src/Python
> /home/alang/src/Python/modcopy
> /home/alang/src/Python/modcopy/src
> >>>
>
> What happened when you tried?

I'll be dipped. It worked - the first time, anyway. It wouldn't go a
second round. Don't I have to reset the object somehow?

When I originally tried it, I placed
print root
print dirs
print files
within the for loop, and I ended up with nuthin'.

Or perhaps it flashed by while I wasn't watching, and it wouldn't do it
a second time???


Ray


More information about the Tutor mailing list