pythonglobfnmatch

Negating a fnmatch pattern


Consider this pattern: *.py. It matches all the paths ending with the py extension. Now, is it possible to find a pattern which matches everything else?

I thought this would do it: *[!.][!p][!y], but apparently fnmatch.fnmatch always returns False with this.

I'd like to avoid regexes. I know I could use them, but in this case it isn't possible.


Solution

  • I personally prefer regex, but if you'd prefer fnmatch, you could do:

    if fnmatch.fnmatch(file, '*[!p][!y]'):
            print(file)