Unrecognized backslash escapes in string literals

Chris Angelico rosuav at gmail.com
Sun Feb 22 21:29:28 EST 2015


In Python, unrecognized escape sequences are treated literally,
without (as far as I can tell) any sort of warning or anything. This
can mask bugs, especially when Windows path names are used:

>>> 'C:\sqlite\Beginner.db'
'C:\\sqlite\\Beginner.db'
>>> 'c:\sqlite\beginner.db'
'c:\\sqlite\x08eginner.db'

To a typical Windows user, the two strings should be equivalent - case
insensitive file names, who cares whether you say "Beginner" or
"beginner"? But to Python, one of them will happen to work, the other
will fail badly.

Why is it that Python interprets them this way, and doesn't even give
a warning? What happened to errors not passing silently? Or, looking
at this the other way: Is there a way to enable such warnings/errors?
I can't see one in 'python[3] -h', but if there's some way elsewhere,
that would be a useful thing to recommend to people (I already
recommend running Python 2 with -tt).

ChrisA



More information about the Python-list mailing list