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)

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <travisgriggs@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.121
X-Spam-Level *
X-Spam-Evidence '*H*': 0.76; '*S*': 0.00; 'subject:files': 0.09; 'subject:using': 0.09; '(at': 0.13; 'first)': 0.16; 'i\xe2\x80\x99m': 0.16; 'nearly': 0.23; 'seems': 0.24; 'all.': 0.24; 'subject:/': 0.29; 'says': 0.32; 'surprised': 0.33; 'received:google.com': 0.34; 'message-id:@gmail.com': 0.35; 'could': 0.35; 'to:addr:python-list': 0.35; 'received:172.16': 0.38; 'say': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'subject: (': 0.40; 'helps': 0.61; 'header:Message-Id:1': 0.62; 'course': 0.64; 'subject': 0.68; 'good:': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:content-transfer-encoding:subject:message-id:date :to:mime-version; bh=0NNfcGH+soA1o9D1yAtpKhTZ7F9GYJIqR3OAc8sPJaQ=; b=Xkat4CnAvcQdIUk9PmeQxnWxXH2cDKiESCC4gALEOu9dUFrsJrskOxTh5rgAJeYL63 vQ0FSoizcKkPvMhHWJcJLUoG1dWUfn3mdbLieLq61lEDv2GkNk+DwhOXNEnzUZwu4Tje pPgwimcaU3tVxcQvbxxfXdp4iav2bCigzGYcALX8wki+H62Y20cHLS9rLdr0cDqUIt4B Umy+dkrloNToulSfCt3Fy/uW2BcPDqJHfesMM+SAujDN6VBxViW2nEXXOjneJtOf3Yh6 5qbS7NwWXxXGx0DWTzT2lb122njZN+yaxFew6DmOp0znwH0lHFSWxlvlsYOEIucG4ME8 o/4Q==
X-Received by 10.70.101.168 with SMTP id fh8mr62538111pdb.15.1435012319964; Mon, 22 Jun 2015 15:31:59 -0700 (PDT)
From Travis Griggs <travisgriggs@gmail.com>
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding quoted-printable
Subject Simplest/Idiomatic way to count files in a directory (using pathlib)
Date Mon, 22 Jun 2015 15:31:58 -0700
To Python <python-list@python.org>
Mime-Version 1.0 (Mac OS X Mail 8.2 \(2098\))
X-Mailer Apple Mail (2.2098)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.712.1435012328.13271.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1435012328 news.xs4all.nl 2847 [2001:888:2000:d::a6]:34164
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:93019

Show key headers only | 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