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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

You can escape the slash. Use \ and you get just one slash.

Method 3

You can escape the backslash with another backslash (\), but it won’t look nicer. To solve that, put an r in front of The string to signal a raw string. A raw string will ignore literally everything , which is a bad thing if you also want colored text, and really bad if your string contains some quotes like this:

a_and_b = r”Alice’s friend is Bob. He always says, “howdy” ”

In this case, python will throw a SyntaxError…


If you haven’t figured out how to solve it, here it is:

a_and_b = r”””Alice’s friend is Bob. He always says, “howdy” ”””
In here, we use a multiline string. And if the string has 3 quotes in a row, then that’s easy. Put a space between them and it’s solved:

three_quotes = ’’’ Here are 3 single quotes: ‘ ‘ ‘ ’’’

Method 4

Another simple (and arguably more readable) approach is using string raw format and replacements like so:

import os
path = os.getcwd()
final = r"{0}xulrunner.exe {0}application.ini".format(path)
print(final)

or using the os path method (and a microfunction for readability):

import os

def add_cwd(path):
    return os.path.join( os.getcwd(), path )

xulrunner = add_cwd("xulrunner.exe")
inifile = add_cwd("application.ini")
# in production you would use xulrunner+" "+inifile
# but the purpose of this example is to show a version where you could use any character
# including backslash
final = r"{} {}".format( xulrunner, inifile )
print(final)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x