how to use the string '\\.\'

Tim Golden mail at timgolden.me.uk
Mon Apr 9 13:21:52 CEST 2007


人言落日是天涯,望极天涯不见家 wrote:
> print r'\\.\'
> 
> This line will cause error. I just want to print the
> \\.\
> why the prefix character "r" isn't effective. Thanks!

Because of the way in which Python parses strings,
you can't end a string -- even a raw one -- with
an odd (1, 3, 5 etc) number of backslashes. In
you example, you'd have to do this:

print "\\\\.\\"

although, depending on what the code was doing,
you might be able to use forward slashes instead;
a surprising number of Microsoft's APIs allow
forward slashes as well as backslashes. Not, however,
anything which is interpreted by the command shell
(which sees a forward slash as a flag indicator) nor
typically anything handled by the Shell subsystem.

TJG




More information about the Python-list mailing list