Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

string - using backslash in python (not to escape)

import os
path= os.getcwd()
final= path +'xulrunner.exe ' + path + 'application.ini'
print final

I want the out put:

c:python25xulrunner.exe c:python25application.ini

I don't want backslash to work as string, i mean don't want it to escape or do anything special. But i get an error

Invalid x escape

How can i use a '' as a '' and not an escape?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To answer your question directly, put r in front of the string.

final= path + r'xulrunner.exe ' + path + r'application.ini'

But a better solution would be os.path.join:

final = os.path.join(path, 'xulrunner.exe') + ' ' + 
         os.path.join(path, 'application.ini')

(the backslash there is escaping a newline, but you could put the whole thing on one line if you want)

I will mention that you can use forward slashes in file paths, and Python will automatically convert them to the correct separator (backslash on Windows) as necessary. So

final = path + '/xulrunner.exe ' + path + '/application.ini'

should work. But it's still preferable to use os.path.join because that makes it clear what you're trying to do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...