[Tutor] Puzzled again
Steven D'Aprano
steve at pearwood.info
Wed Aug 3 11:02:34 CEST 2011
Steven D'Aprano wrote:
> Richard D. Moores wrote:
>> Puzzled again. Why the error. Line 36 is the line just above "import
>> os.path". I have many other functions in mycalc.py with examples
>> formatted exactly the same way.
>>
>> def convertPath(path):
>> """
>> Given a path with backslashes, return that path with forward slashes.
>>
>> By Steven D'Aprano 07/31/2011 on Tutor list
>> >>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>
> Are you aware that this is not a raw string? It's wrapped inside another
> non-raw string, so it is merely a sub-string containing the letters r
> single-quote C colon backslash-U etc. Now, as it turns out, backslash-U
> and friends don't have any special meanings, so it will work fine, but
*slaps head*
Doh! No, I was wrong. \U DOES have a special meaning. It introduces an
eight-character unicode escape sequence, but only in unicode strings.
>>> u'\Users\Dick'
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 0-2: truncated \UXXXXXXXX escape
This fails because the \U is not followed by any valid hex digits. So in
Python 3, you should expect a different syntax error.
--
Steven
More information about the Tutor
mailing list