Simplest/Idiomatic way to count files in a directory (using pathlib)
Terry Reedy
tjreedy at udel.edu
Tue Jun 23 12:17:34 EDT 2015
On 6/22/2015 9:32 PM, Paul Rubin wrote:
> Travis Griggs <travisgriggs at gmail.com> writes:
>> The following seems to obtuse/clever for its own good:
>> return sum(1 for _ in self.path.iterdir())
I disagree. For one who understands counting and Python, this is a
direct way to define the count of a finite iterable. A functionalist
would prefer it to the obvious 3-line version with a for statement.
> I've generally done something like that. I suppose it could be added to
> itertools.
itertools callables all produce iterators. The above only consumes an
iterable.
--
Terry Jan Reedy
More information about the Python-list
mailing list