raw strings under windows

Alex Martelli aleax at aleax.it
Sun Jun 15 04:12:35 EDT 2003


<posted & mailed>

Cecil H. Whitley wrote:

> Hi,
> When doing the following:
> 
> #!/usr/bin/env python
> 
> path = r"c:\python23\"
> 
> I get a syntax error, unexpected EOL with singlequoted string.  It was my
> (mis?) understanding that raw strings did not process escaped characters?

They don't, in that the backslash remains in the string resulting from
the raw literal, BUT so does the character right after the backslash,
unconditionally.  As a result, a raw string literal cannot end with an
odd number of backslashes.  If they did otherwise, it would instead be
impossible to include a single quote character in a single-quoted raw
string literal, etc.  Raw string literals are designed mainly to ease
the task of entering regular expressions, and for that purpose an odd
number of ending backslashes is never needed, while making inclusion of
quote characters harder _would_ be an issue, so the design choice was
easy to make.

Of course people who use raw string literals to represent DOS paths might
wish otherwise, but as has been pointed out it's not a big problem in
any case -- not only, as you note:

> Of course
> path = "c:\\python23\\"
> 
> works just fine.

but so, almost invariably, does 'c:/python23/' (Microsoft's C runtime
libraries accept / interchangeably with \ as part of file path syntax,
and Python relies on the C runtime libraries and so does likewise).


Alex





More information about the Python-list mailing list