Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93021
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.028 |
| X-Spam-Evidence | '*H*': 0.94; '*S*': 0.00; 'counting': 0.07; '22,': 0.09; 'subject:files': 0.09; 'subject:using': 0.09; '(at': 0.13; 'def': 0.14; 'finite,': 0.16; 'first)': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'iterators': 0.16; 'i\xe2\x80\x99m': 0.16; 'len': 0.16; 'travis': 0.16; 'wrote:': 0.16; 'skip:l 30': 0.18; '2015': 0.23; 'nearly': 0.23; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'mon,': 0.24; 'yield': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'subject:/': 0.29; 'work.': 0.30; 'says': 0.32; 'surprised': 0.33; 'received:google.com': 0.34; 'could': 0.35; 'to:addr:python- list': 0.35; 'list': 0.35; 'subject:: ': 0.37; 'files': 0.38; 'pm,': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.40; 'further': 0.60; 'even': 0.61; 'helps': 0.61; 'necessarily': 0.61; 'simple': 0.61; 'entire': 0.61; 'course': 0.64; 'subject': 0.68; 'good:': 0.84; 'idiom': 0.84; 'reasons:': 0.84; 'to:name:python': 0.84; 'yielded': 0.84; 'preventing': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=weaZZUXLwaDZpV88p1YoqR70aYgSKRpfrryHBXDosZ4=; b=ukv05p9NvIRNH6DDUaRrPAzCympAxQqetEGme6axK2Rq8uC/+6mdjP8K2kli2Z4T/l tk+0OntMgYYcyJqmvjS107jhbThdbdbZQPXmhfyEkAdSD2/t7caWALFPiTX5PPpjWyc3 Cm/yrs4Of186MDJ3bPQWyVd98oUYlMBcLRHk2wEiefszC4lbU41A24km+/2ji5UCroeo OjGEn4A1jJJAhRQyv83itgAS4V/F1Ch0FCm6gEss5EasXsGK+tNr/cwE9OUdeOfgMaYN QlFsaZT2IDjhHAPPkBtL8AkcqITNgk+PR2ToAK9vciOq8laLmeuhuiZiXG65sC/DDO6z Nn1g== |
| X-Received | by 10.170.42.85 with SMTP id 82mr39704723ykk.18.1435013342555; Mon, 22 Jun 2015 15:49:02 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <E2B820B1-2238-4914-BD20-02AAF60F3243@gmail.com> |
| References | <8B8C6022-75BE-41FC-9B18-7D89AD34A35A@gmail.com> <E2B820B1-2238-4914-BD20-02AAF60F3243@gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Mon, 22 Jun 2015 16:48:23 -0600 |
| Subject | Re: Simplest/Idiomatic way to count files in a directory (using pathlib) |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| 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.714.1435013345.13271.python-list@python.org> (permalink) |
| Lines | 42 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1435013345 news.xs4all.nl 2901 [2001:888:2000:d::a6]:41931 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:93021 |
Show key headers only | View raw
On Mon, Jun 22, 2015 at 4:33 PM, Travis Griggs <travisgriggs@gmail.com> wrote:
> <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.
len doesn't work on iterators for a number of reasons:
* It would exhaust the iterator, preventing further use.
* The iterator is not necessarily finite.
* Even if it's finite, the length is not necessarily deterministic.
Consider this simple generator:
def gen():
while random.randrange(2):
yield 42
> 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())
This is the usual idiom for counting the number of items yielded from
an iterator. Alternatively you can use len(list(self.path.iterdir()))
if you don't mind constructing a list of the entire directory listing.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Simplest/Idiomatic way to count files in a directory (using pathlib) Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-22 16:48 -0600
csiph-web