Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.06; 'everybody,': 0.07; 'fname': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'sucks': 0.09; 'def': 0.12; 'etc....': 0.16; 'path.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:raw': 0.16; 'traverse': 0.16; 'files.': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'basically': 0.19; 'skip:+ 20': 0.22; 'header:User-Agent:1': 0.23; 'file.': 0.24; 'looks': 0.24; "i've": 0.25; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'xml': 0.29; 'skip:+ 30': 0.30; 'skip:( 20': 0.30; 'directory,': 0.31; 'file': 0.32; 'class': 0.32; 'actual': 0.34; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'one,': 0.35; 'prepare': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'done': 0.36; 'doing': 0.36; 'charset :us-ascii': 0.36; 'similar': 0.36; 'so,': 0.37; 'too': 0.37; 'skip:- 20': 0.37; 'two': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'mine': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'skip:- 10': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'manually': 0.60; 'new': 0.61; 'course': 0.61; 'simple': 0.61; 'name': 0.63; 'different': 0.65; 'production': 0.68; 'adapted': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neil Cerutti Subject: Re: Python glob and raw string Date: Thu, 16 Jan 2014 18:14:30 +0000 (UTC) Organization: Norwich University References: <6d3dd7d7-6836-4b55-9d1c-9e70e18b66fd@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: jackman.norwich.edu User-Agent: slrn/0.9.9p1/mm/ao (Win32) 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389896098 news.xs4all.nl 2942 [2001:888:2000:d::a6]:59886 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64096 On 2014-01-16, Xaxa Urtiz wrote: > Hello everybody, i've got a little problem, i've made a script > which look after some files in some directory, typically my > folder are organized like this : > > [share] > folder1 > ->20131201 > -->file1.xml > -->file2.txt > ->20131202 > -->file9696009.tmp > -->file421378932.xml > etc.... > so basically in the share i've got some folder > (=folder1,folder2.....) and inside these folder i've got these > folder whose name is the date (20131201,20131202,20131203 > etc...) and inside them i want to find all the xml files. > So, what i've done is to iterate over all the folder1/2/3 that > i want and look, for each one, the xml file with that: > > for f in glob.glob(dir +r"\20140115\*.xml"): > ->yield f > > dir is the folder1/2/3 everything is ok but i want to do > something like that : > > for i in range(10,16): > ->for f in glob.glob(dir +r"\201401{0}\*.xml".format(i)): > -->yield f > > but the glob does not find any file.... (and of course there is > some xml and the old way found them...) > Any help would be appreciate :) I've done this two different ways. The simple way is very similar to what you are now doing. It sucks because I have to manually maintain the list of subdirectories to traverse every time I create a new subdir. Here's the other way, using glob and isdir from os.path, adapted from actual production code. class Miner: def __init__(self, archive): # setup goes here; prepare to acquire the data self.descend(os.path.join(archive, '*')) def descend(self, path): for fname in glob.glob(os.path.join(path, '*')): if os.path.isdir(fname): self.descend(fname) else: self.process(fname) def process(self, path): # Do what I want done with an actual file path. # This is where I add to the data. In your case you might not want to process unless the path also looks like an xml file. mine = Miner('myxmldir') Hmmm... I might be doing too much in __init__. ;) -- Neil Cerutti