how to use the string '\\.\'

G

Guest

print r'\\.\'

This line will cause error. I just want to print the
\\.\
why the prefix character "r" isn't effective. Thanks!
 
T

Tim Golden

äººè¨€è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ said:
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
 
D

Duncan Booth

Gabriel Genellina said:
En Mon, 09 Apr 2007 08:21:44 -0300, Duncan Booth


This form is not included in the FAQ alternatives (should it?):

print r'\\.' '\\'
Yes it is. It is just the first of the FAQ suggestions using single quotes
instead of double quotes. If you wanted to add another one then I would go
for:

os.path.normpath("/this/is/my/dos/dir")

although it doesn't work with the particular example in question here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top