[New-bugs-announce] [issue6665] fnmatch fails on filenames containing \n character

Josef Skladanka report at bugs.python.org
Fri Aug 7 11:49:19 CEST 2009

New submission from Josef Skladanka <jskladan at redhat.com>:

at the moment, fnmatch.fnmatch() will fail to match any string, which
has \n character. This of course breaks glob as well.


> import fnmatch
> fnmatch.fnmatch("foo\nbar", "foo*")

> import glob
> open("foobar", "w").close()
> open("foo\nbar", "w").close()
> glob.glob("foo*")

while the expected result is ['foobar', 'foo\nbar']. The standard C
fnmatch function from fnmatch.h is behaving correctly i.e. this code
will print out "match!"

#include <fnmatch.h>
#include <stdio.h>

int main()
    if (fnmatch("foo*", "foo\nbar", FNM_NOESCAPE) == 0)
    return 0;

This misbehaviour is caused by the fnmatch.translate() which adds $ to
the end of the regexp. Without the ending $ the fnmatch function works OK.

components: Library (Lib)
messages: 91398
nosy: rajcze
severity: normal
status: open
title: fnmatch fails on filenames containing \n character
type: behavior
versions: Python 2.4, Python 2.5, Python 2.6, Python 2.7, Python 3.0, Python 3.1, Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list