Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'sufficient': 0.05; 'that?': 0.05; 'plenty': 0.07; 'suppose': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'pep': 0.09; '~ethan~': 0.09; 'systems.': 0.12; 'empty.': 0.16; 'finney': 0.16; 'measured': 0.16; 'simplest': 0.16; 'subject: \n ': 0.16; 'thereby': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'header :User-Agent:1': 0.23; 'certainly': 0.24; 'entries': 0.24; 'exists': 0.24; 'extension': 0.26; 'header:In-Reply-To:1': 0.27; 'getting': 0.31; 'directory,': 0.31; 'subject:that': 0.31; 'writes:': 0.31; 'file': 0.32; '(e.g.': 0.33; 'something': 0.35; 'test': 0.35; 'done,': 0.36; 'subject:?': 0.36; 'should': 0.36; 'too': 0.37; 'list': 0.37; 'performance': 0.37; 'skip:o 20': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'pm,': 0.38; 'realize': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'even': 0.60; 'easy': 0.60; '8bit%:29': 0.60; 'simple,': 0.60; 'received:173': 0.61; 'back': 0.62; 'decided': 0.64; 'more': 0.64; 'determine': 0.67; 'goal': 0.75; 'beverage,': 0.84; 'subject:Test': 0.84; 'subject:very': 0.91; 'walking': 0.91 Date: Thu, 07 Aug 2014 00:30:27 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Test for an empty directory that could be very large if it is not empty? References: <53E2A22A.10906@it.uu.se> <854mxpb6fq.fsf@benfinney.id.au> In-Reply-To: <854mxpb6fq.fsf@benfinney.id.au> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407396629 news.xs4all.nl 2898 [2001:888:2000:d::a6]:53452 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75839 On 08/06/2014 03:26 PM, 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? > > What is your goal for that? Have you measured the performance difference > and decided *based on objective observation* that it's too expensive? > > Certainly ‘os.listdir(foo)’ is the simplest way to determine 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? Plenty of people have measured the slowdown of getting a list of files when the directory has thousands upon thousands. It's why the scandir PEP exists at all, and the slowdown is present even on local file systems. While it may not be objective, walking away to get a cup of your favorite beverage, coming back and seeing the operation is still not done, is certainly sufficient to realize that the simple, easy way is not going to be sufficient. -- ~Ethan~