Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.033 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'python': 0.09; 'received :mail-vc0-f174.google.com': 0.09; 'subject:files': 0.09; 'dec': 0.15; '2:28': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gilles': 0.16; 'googled': 0.16; 'iterator': 0.16; 'reasonably': 0.16; 'wrote:': 0.17; 'memory': 0.18; 'header :In-Reply-To:1': 0.25; 'creating': 0.26; 'am,': 0.27; 'in.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'giant': 0.29; 'received:209.85.220.174': 0.29; 'yields': 0.29; "i'm": 0.29; 'problem.': 0.32; 'url:python': 0.32; 'file': 0.32; 'structure': 0.32; 'says': 0.33; 'surely': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'kind': 0.61; 'customer': 0.61; 'potentially': 0.66; 'sounds': 0.71; 'subject:over': 0.84; 'zen': 0.84; 'hundred': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=2yImHUj9MmJpxOhnXuGGlw8KzeHS0bWlHJKfMJd2kHQ=; b=RfRRhkipcgzpSgnwZ9BUMCDY37OpdDJR5YmTXYo8SzVOfwwJ4XwbnLc+bPHYo3KLrV Igjzs8Es177W16S4oacCK+QuQFdFY0EiFvY9MBu0L+DmTGKGJfXM8yNMGBYGS0dfDcp4 iiKJHNNjo8WFqR2m4ypYp/6apzvgfFFmFR51hZeDSHC+9JWq+94T0OIy13+wHSQq5bHn awp7pakmVg36N/P0Dl6/nG4/4AmvaY3iVndQSH9da/DR9D2DyIvWDA1pSEfk3Ngnlhmv 8PHHeNQKvyiVZ6a1kv1zhbfXsjqgksgrlTx/BiBOzSAJ4b4Ad8fgyAjpVEVE+VIWdoJi NKsQ== MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 18 Dec 2012 02:41:42 +1100 Subject: Re: Iterating over files of a huge directory From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 16 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355758912 news.xs4all.nl 6887 [2001:888:2000:d::a6]:56690 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34980 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... 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? http://docs.python.org/3.3/library/os.html#os.walk ChrisA