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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'predefined': 0.07; 'problem?': 0.07; 'python': 0.09; 'subject:files': 0.09; 'stored': 0.10; 'dec': 0.15; '2:28': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'gilles': 0.16; 'googled': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'os.walk': 0.16; 'reasonably': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'directory.': 0.17; 'package.': 0.17; 'creates': 0.18; '>>>': 0.18; 'memory': 0.18; 'written': 0.20; "i've": 0.23; 'external': 0.24; 'paul': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'creating': 0.26; 'ago': 0.27; 'am,': 0.27; 'implemented': 0.27; 'separate': 0.27; 'disk': 0.27; 'in.': 0.27; "doesn't": 0.28; 'chris': 0.28; 'initial': 0.28; 'run': 0.28; '100000': 0.29; 'giant': 0.29; 'in-house': 0.29; 'received:192.168.1.3': 0.29; 'writes:': 0.29; 'yields': 0.29; 'probably': 0.29; "i'm": 0.29; 'query': 0.30; 'problem.': 0.32; 'file': 0.32; 'structure': 0.32; 'certain': 0.33; 'says': 0.33; 'directory,': 0.33; 'extract': 0.33; 'surely': 0.33; 'utility': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'list': 0.35; 'needed': 0.35; 'sometimes': 0.35; 'something': 0.35; 'list.': 0.35; 'but': 0.36; 'smaller': 0.36; "wasn't": 0.36; 'too': 0.36; 'bad': 0.37; 'does': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'space': 0.39; 'called': 0.39; 'application': 0.40; 'received:192.168': 0.40; 'save': 0.61; 'kind': 0.61; 'customer': 0.61; 'potentially': 0.66; 'header:Reply-To:1': 0.68; 'sounds': 0.71; 'reply-to:no real name:2**0': 0.72; 'designed,': 0.84; 'reply-to:addr:python.org': 0.84; 'subject:over': 0.84; 'zen': 0.84; 'hundred': 0.95 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=_OHkB4WUJzwA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=yJlm2WYYQsIA:10 a=pGLkceISAAAA:8 a=6VBBHSAAA2fFFWNY1mEA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=lzOIaz7J_ECAHJUf:21 a=0MrK6a41zsSBW_Ee:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 17 Dec 2012 18:29:46 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Iterating over files of a huge directory References: <87d2y8io7i.fsf@no-fixed-abode.cable.virginmedia.net> In-Reply-To: <87d2y8io7i.fsf@no-fixed-abode.cable.virginmedia.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355768986 news.xs4all.nl 6864 [2001:888:2000:d::a6]:53483 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35003 On 2012-12-17 17:27, Paul Rudin wrote: > Chris Angelico writes: > >> On Tue, Dec 18, 2012 at 2: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. >>> >>> 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. >> >> Sounds like you want os.walk. > > But doesn't os.walk call listdir() and that creates a list of the > contents of a directory, which is exactly the initial problem? > >> But... a hundred thousand files? I know the Zen of Python says that >> flat is better than nested, but surely there's some kind of directory >> structure that would make this marginally manageable? >> > > Sometimes you have to deal with things other people have designed, so > the directory structure is not something you can control. I've run up > against exactly the same problem and made something in C that > implemented an iterator. > Years ago I had to deal with an in-house application that was written using a certain database package. The package stored each predefined query in a separate file in the same directory. I found that if I packed all the predefined queries into a single file and then called an external utility to extract the desired query from the file every time it was needed into a file for the package to use, not only did it save a significant amount of disk space (hard disks were a lot smaller then), I also got a significant speed-up! It wasn't as bad as 100000 in one directory, but it was certainly too many... > It would probably be better if listdir() made an iterator rather than a > list. >