r'\' - python parser bug?

Duncan Booth me at privacy.net
Wed May 26 04:10:27 EDT 2004


michael at foord.net (Fuzzyman) wrote in 
news:8089854e.0405252339.18e0c59d at posting.google.com:

>>>> print r'c:\subdir\'
> SyntaxError: EOL while scanning single-quoted string
>>>> 
> 
>> When the parser sees a backslash inside
>> a string literal, it always skips the next character. 
> In the above example the parser *only* skips the next character if it
> is at the end of the string... surely illogical. The reason given is
> effectively 'raw strings were created for regular expressions, so it
> doesn't matter if the behaviour is illogical' (and precludes other
> reasonable uses!!)..........
> 

In a python string, backslash is an escape character which gives the next 
character(s) special meaning, so '\n' is a single newline character. If the 
escaped character isn't a known escape then the parser simply passes 
through the entire sequence. So '\s' is a two character string. In all 
cases at least one character following the backslash is parsed when the 
backslash is encountered, and this character can never form part of the 
string terminator.

Raw strings are processed in exactly the same way as normal strings, except 
that no escape sequences are recognised, however the character following 
the backslash is still prevented from terminating the string, just as it 
would in any other string. This *useful*? behaviour allows you to put 
single and double quotes into a raw string provided that they are preceded
by a backslash.

  print r'c:\subdir\'file'

Raw strings aren't intended for writing DOS pathnames, they are actually 
targetted for regular expressions where this behaviour makes more sense.

If you need a lot of pathnames in your program you could consider using 
forward slash as the directory separator (use os.path.normpath to convert 
to backslashes if you feel the need), or put all your paths in a separate 
configuration file where you can choose what quoting, if any to interpret.

Also, provided you use os.path.join to concatenate paths you never actually 
*need* to include a trailing separator:

   DIR = r'c:\subdir'
   FILE = os.path.join(DIR, 'filename')

ducks the entire issue cleanly.



More information about the Python-list mailing list