Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93019

Simplest/Idiomatic way to count files in a directory (using pathlib)

From Travis Griggs <travisgriggs@gmail.com>
Subject Simplest/Idiomatic way to count files in a directory (using pathlib)
Date 2015-06-22 15:31 -0700
Newsgroups comp.lang.python
Message-ID <mailman.712.1435012328.13271.python-list@python.org> (permalink)

Show all headers | View raw


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())

I don’t say anything on the 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())

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Simplest/Idiomatic way to count files in a directory (using pathlib) Travis Griggs <travisgriggs@gmail.com> - 2015-06-22 15:31 -0700
  Re: Simplest/Idiomatic way to count files in a directory (using pathlib) Paul Rubin <no.email@nospam.invalid> - 2015-06-22 18:32 -0700
    Re: Simplest/Idiomatic way to count files in a directory (using pathlib) Terry Reedy <tjreedy@udel.edu> - 2015-06-23 12:17 -0400

csiph-web