count files in a directory

Steven Bethard steven.bethard at gmail.com
Sat May 21 13:24:18 EDT 2005


rbt wrote:
> Heiko Wundram wrote:
>>>>> import os
>>>>> path = "/home/heiko"
>>>>> file_count = sum((len(f) for _, _, f in os.walk(path)))
>>>>> file_count
> 
> Thanks! that works great... is there any significance to the underscores 
> that you used? I've always used root, dirs, files when using os.walk() 
> do the underscores make it faster... or more efficient?

No, they don't make any semantic difference; they work just like any 
other identifier.  It's just that, by convention, single underscores 
indicate that we don't care what values these names are bound to.  So in 
the example above, Heiko indicates that we don't care about what you 
would normally call 'root' and 'dirs'.

HTH,

STeVe



More information about the Python-list mailing list