Simplest/Idiomatic way to count files in a directory (using pathlib)
Travis Griggs
travisgriggs at gmail.com
Mon Jun 22 18:33:18 EDT 2015
<I should proof my posts before I send them, sorry>
Subject nearly says it all.
If i’m using pathlib, what’s the simplest/idiomatic way to simply count how many files are in a given directory?
I was surprised (at first) when
len(self.path.iterdir())
didn’t work.
I don’t see anything in the .stat() object that helps me.
I could of course do the 4 liner:
count = 0
for _ in self.path.iterdir():
count += 1
return count
The following seems to obtuse/clever for its own good:
return sum(1 for _ in self.path.iterdir())
More information about the Python-list
mailing list