pythonfnmatch

Is fnmatch.fnmatch incompatible to the Unix pattern filematching syntax?


I am using Python 3.7.6 and the fnmatch.fnmatch function to match a filename with a given pattern. Most of my tests worked, but in specific the following example doesn't return the value as expected. Give is the following exanoke:

> fnmatch.fnmatch('_foo\bar.exe', '^_*')
False

What I try to do, to match any filename whose component starts with _. Any help is highly appreciated


Solution

  • This is not a place where Python fails to comply with the POSIX specification. UNIX pattern matching syntax does not require ^ to have any special meaning, except in a square-bracket character-set description.

    See:


    This makes sense: Globs are implicitly anchored -- they always match only at the beginning, so there's no reason to support an explicit anchor.