parsing directory for certain filetypes
Gerard Flanagan
grflanagan at gmail.com
Tue Mar 11 04:56:05 EDT 2008
On Mar 11, 6:21 am, royG <roygeor... at gmail.com> wrote:
> On Mar 10, 8:03 pm, Tim Chase wrote:
>
> > In Python2.5 (or 2.4 if you implement the any() function, ripped
> > from the docs[1]), this could be rewritten to be a little more
> > flexible...something like this (untested):
>
> that was quite a good lesson for a beginner like me..
> thanks guys
>
> in the version using glob()
>
> >path = os.path.normpath(os.path.join(folder, '*.txt'))
> >lst = glob.glob(path)
>
> is it possible to check for more than one file extension? here i will
> have to create two path variables like
> path1 = os.path.normpath(os.path.join(folder, '*.txt'))
> path2 = os.path.normpath(os.path.join(folder, '*.doc'))
>
> and then use glob separately..
> or is there another way?
>
I don't think you can match multiple patterns directly with glob, but
`fnmatch` - the module used by glob to do check for matches - has a
`translate` function which will convert a glob pattern to a regular
expression (string). So you can do something along the lines of the
following:
---------------------------------------------
import os
from fnmatch import translate
import re
d = '/tmp'
patt1 = '*.log'
patt2 = '*.ini'
patterns = [patt1, patt2]
rx = '|'.join(translate(p) for p in patterns)
patt = re.compile(rx)
for f in os.listdir(d):
if patt.match(f):
print f
---------------------------------------------
hth
Gerard
More information about the Python-list
mailing list