Python Question

glob exclude pattern

I have a directory with a bunch of files inside:

eee2314
,
asd3442
... and
eph
.

I want to exclude all files that start with
eph
with the
glob
function.

How can I do it?

Answer

You can't exclude patterns with the glob function, globs only allow for inclusion patterns. Globbing syntax is very limited (even a [!..] character class must match a character).

You'll have to do your own filtering; a list comprehension usually works nicely here:

files = [fn for fn in glob('somepath/*.txt') 
         if not os.path.basename(fn).startswith('eph')]
Comments