[Python-Dev] Emit SyntaxWarning on unrecognized backslash escapes?

Terry Reedy tjreedy at udel.edu
Tue Feb 24 21:26:35 CET 2015


On 2/24/2015 1:14 PM, Guido van Rossum wrote:

> And I'd weigh the needs of users who know what they are doing somewhat
> higher than educating newbies through error messages. While newbies are
> most likely to try out open() with a string literal, in "real" programs
> that is rare, and filenames are typically taken from the command line or
> from some other source where the peculiarities of Python string literals
> are irrelevant.

I have been thinking about proposing a new how-to: Understanding Error 
Messages, with a section on tracebacks followed by an alphabetical 
listing of Exceptions that gives people problems, with possible 
solutions for each.  The following begins my first draft.

FileNotFoundError:

Possible cause [Windows]: You used a normal string literal to create a 
filename, you used '\' as the path separator, and you forgot that Python 
(like most languages) treats '\' as a case-sensitive escape character. 
For example: "C:\Test" is 7 chars and works as a file name, while 
'C:\test' is 6 chars, one a literal tab character.  The latter does not 
work as a file name and will raise FileNotFoundError.

Possible solutions: 1. Use raw string literals for Windows path names 
(r'C:\test'). 2 (recommended). Use '/' as the path separator 
('C:/test'), just as one does on other systems. This always works when 
passing file names from Python to Windows, even though it sometimes does 
not work in Windows Command Prompt console.


-- 
Terry Jan Reedy



More information about the Python-Dev mailing list