glob & fnmatch unable to match dirs/files containing [ ] ?

Robert k.robert at gmx.de
Sat Mar 22 11:24:01 EST 2003


glob/fnmatch uses [seq] matching.
On Windows and Linux brackets are allowed in filenames:

04.06.2002  16:24    <DIR>          CVS
08.11.2002  21:49    <DIR>          testsync[2]        (contains files)

>>> glob.glob('testsync[2]/*')
[]

obviously would match only on testsync2.
adding 'testsync2':

04.06.2002  16:24    <DIR>          CVS
08.11.2002  21:49    <DIR>          testsync[2]        (contains files)
21.03.2003  15:43    <DIR>          testsync2           (contains other
files)

>>> glob.glob('testsync[2]/*')
['testsync2\\x.txt', 'testsync2\\y.txt', 'testsync2\\z.txt',
'testsync2\\testsync.syncpy.syncdb']

delivers the content of the other dir testsync2 !

>>> fnmatch.fnmatch('testsync[2]','testsync[2]')
0

>>> fnmatch.fnmatch('testsync2','testsync[2]')
1

quoting attempts don't work:

>>> glob.glob(  r'testsync\[2\]/*'  )            # or  'testsync\\[2\\]/*'
[]

This would work in the linux shell like:  ro at lx:~/acer > ll testsync\[2\]/*
#works
Maybe this quote should be supported also in future glob/fnmatch versions?

Does anybody know a simple remedy. the huge problems come when such
dir/filenames are part of multipart subdir&file-patterns for glob. e.g.:
glob.glob("x:/backup/testsync[2]/data/db[4]usr*.csv")

Robert







More information about the Python-list mailing list