Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'paths': 0.05; 'sys': 0.05; 'bytes.': 0.07; 'filename': 0.07; 'prefix': 0.07; 'problem?': 0.07; '172': 0.09; 'bytes,': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:files': 0.09; 'terry': 0.09; 'def': 0.10; '3.3.': 0.16; 'do)': 0.16; 'gilles': 0.16; 'googled': 0.16; 'i.e': 0.16; 'iterator': 0.16; 'reasonably': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'char': 0.17; 'issue,': 0.17; 'unicode': 0.17; 'jan': 0.18; 'memory': 0.18; 'issue.': 0.20; 'import': 0.21; 'example': 0.23; 'url:bugs': 0.24; 'machine': 0.24; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'developers': 0.26; 'creating': 0.26; 'am,': 0.27; 'guess': 0.27; '3.0': 0.27; 'entries': 0.27; 'in.': 0.27; 'skip:e 30': 0.27; 'header:X -Complaints-To:1': 0.28; 'post': 0.28; '100000': 0.29; 'giant': 0.29; 'yields': 0.29; "i'm": 0.29; '(and': 0.32; 'problem.': 0.32; 'url:python': 0.32; 'file': 0.32; 'space,': 0.32; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'entry': 0.33; 'changed': 0.34; 'list': 0.35; 'needed': 0.35; 'path': 0.35; 'sequence': 0.35; 'list.': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'smaller': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'enough': 0.36; 'does': 0.37; 'item': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'there,': 0.38; 'to:addr:python.org': 0.39; 'space': 0.39; 'short': 0.39; 'application': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'claim': 0.60; 'real': 0.61; 'personally': 0.61; 'customer': 0.61; 'relatively': 0.62; 'more': 0.63; 'potentially': 0.66; 'below.': 0.68; 'yourself': 0.77; '100': 0.78; 'received:fios.verizon.net': 0.84; 'subject:over': 0.84; 'wash': 0.84; 'responses': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Iterating over files of a huge directory Date: Mon, 17 Dec 2012 16:27:22 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355779656 news.xs4all.nl 6951 [2001:888:2000:d::a6]:55586 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35016 On 12/17/2012 10:28 AM, Gilles Lenfant wrote: > Hi, > > I have googled but did not find an efficient solution to my problem. > My customer provides a directory with a huuuuge list of files (flat, > potentially 100000+) and I cannot reasonably use > os.listdir(this_path) unless creating a big memory footprint. Is is really big enough to be a real problem? See below. > So I'm looking for an iterator that yields the file names of a > directory and does not make a giant list of what's in. > > i.e : > > for filename in enumerate_files(some_directory): # My cooking... See http://bugs.python.org/issue11406 As I said there, I personally think (and still do) that listdir should have been changed in 3.0 to return an iterator rather than a list. Developers who count more than me disagree on the basis that no application has the millions of directory entries needed to make space a real issue. They also claim that time is a wash either way. As for space, 100000 entries x 100 bytes/entry (generous guess at average) = 10,000,000 bytes, no big deal with gigabyte memories. So the logic goes. A smaller example from my machine with 3.3. from sys import getsizeof def seqsize(seq): "Get size of flat sequence and contents" return sum((getsizeof(item) for item in seq), getsizeof(seq)) import os d = os.listdir() print(seqsize([1,2,3]), len(d), seqsize(d)) # 172 45 3128 The size per entry is relatively short because the two-level directory prefix for each path is only about 15 bytes. By using 3.3 rather than 3.0-3.2, the all-ascii-char unicode paths only take 1 byte per char rather than 2 or 4. If you disagree with the responses on the issue, after reading them, post one yourself with real numbers. -- Terry Jan Reedy