Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:not': 0.03; 'algorithm': 0.04; 'suppose': 0.07; 'cc:addr:python-list': 0.11; 'changes': 0.15; '-tkc': 0.16; 'empty.': 0.16; 'finney': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'likewise': 0.16; 'simplest': 0.16; 'subject: \n ': 0.16; 'thereby': 0.16; 'wrote:': 0.18; '(in': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'entries': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'extension': 0.26; 'header:In-Reply-To:1': 0.27; 'directory,': 0.31; 'subject:that': 0.31; 'writes:': 0.31; 'file': 0.32; '(e.g.': 0.33; 'something': 0.35; 'test': 0.35; "didn't": 0.36; 'subject:?': 0.36; 'should': 0.36; 'behind': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'ben': 0.38; 'structure': 0.39; 'either': 0.39; 'how': 0.40; '8bit%:29': 0.60; 'serving': 0.60; 'more': 0.64; 'determine': 0.67; 'believe': 0.68; 'received:50.22': 0.84; 'scenes': 0.84; 'subject:Test': 0.84; 'timings': 0.84; 'subject:very': 0.91 Date: Wed, 6 Aug 2014 17:44:41 -0500 From: Tim Chase To: Ben Finney Subject: Re: Test for an empty directory that could be very large if it is not empty? In-Reply-To: <854mxpb6fq.fsf@benfinney.id.au> References: <53E2A22A.10906@it.uu.se> <854mxpb6fq.fsf@benfinney.id.au> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407365165 news.xs4all.nl 2837 [2001:888:2000:d::a6]:33477 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75815 On 2014-08-07 08:26, Ben Finney wrote: > Virgil Stokes writes: > > Suppose I have a directory C:/Test that is either empty or > > contains more than 2000000 files, all with the same extension > > (e.g. *.txt). How can I determine if the directory is empty > > WITHOUT the generation of a list of the file names in it (e.g. > > using os.listdir('C:/Test')) when it is not empty? >=20 > Certainly =E2=80=98os.listdir(foo)=E2=80=99 is the simplest way to determ= ine the > entries in a directory, and thereby to test whether it is empty. > That simplicity is very valuable, and you should have a compelling, > *measured* reason to do something more complicated. What is it? With all the changes in 2->3 where many listy things were made into iteratory things (e.g. range()), I was surprised that os.listdir() didn't do likewise since I believe that just about every OS uses some iterator-like call behind the scenes anyways. The difference in timings when serving a web-request are noticeable (in my use-case, I had to change my algorithm and storage structure to simplify/avoid heavily-populated directories) -tkc