[Tutor] glob.glob(pattern, dir) ?

spir denis.spir at free.fr
Tue Jun 9 20:23:45 CEST 2009

Le Tue, 9 Jun 2009 17:40:26 +0200,
Sander Sweers <sander.sweers at gmail.com> s'exprima ainsi:

> 2009/6/9 spir <denis.spir at free.fr>:
> > is there a way to make glob work in a specified dir?
> glob needs a pathname and uses the current dir if no full path is
> given. Prepend the path to your match (*.txt in example below) and it
> works like you want it.
> >>> import glob
> >>> glob.glob('c:\\GTK\\*.txt')
> ['c:\\GTK\\license.txt']
> Greets
> Sander

Thank you. That was so simple I couldn't figure it out myself ;-)

What I was expecting is:

import os, glob
def dirglob(dir, pattern):
   fullPattern = os.path.join(dir,pattern)
   return glob.glob(fullPattern)

(used e.g.
   print dirglob("/home/spir/prog/*/doc", "*.txt")
to get .txt files from only doc subdirs of all projects)

la vita e estrany

More information about the Tutor mailing list