41

I have this code:

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

I want output like:

C:\Users\me\xulrunner.exe C:\Users\me\application.ini

But instead I get an error that looks like:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

I don't want the backslashes to be interpreted as escape sequences, but as literal backslashes. How can I do it?


Note that if the string should only contain a backslash - more generally, should have an odd number of backslashes at the end - then raw strings cannot be used. Please use How can I get a string with a single backslash in it? to close questions that are asking for a string with just a backslash in it. Use How to write string literals in python without having to escape them? when the question is specifically about wanting to avoid the need for escape sequences.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
esafwan
  • 17,311
  • 33
  • 107
  • 166
  • See also [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) if you are trying to make a string with a file path in it on Windows. – Karl Knechtel Aug 07 '22 at 02:35
  • There is a reasonably good overview of escape sequences [here](https://stackoverflow.com/a/31213916/523612), although that question isn't actually good quality - the underlying problem described by OP isn't actually anything to do with the escape sequences. – Karl Knechtel Aug 07 '22 at 03:36
  • See also: [python: SyntaxError: EOL while scanning string literal](https://stackoverflow.com/questions/3561691/) – Karl Knechtel Aug 07 '22 at 04:16
  • See also: [How to fix " DeprecationWarning: invalid escape sequence" in Python?](https://stackoverflow.com/questions/52335970) – Karl Knechtel Aug 07 '22 at 04:53

4 Answers4

51

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.

David Z
  • 128,184
  • 27
  • 255
  • 279
34

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

avacariu
  • 2,780
  • 3
  • 25
  • 25
4

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 all escape sequences, treating backslashes as literal text. It cannot contain the closing quote unless it is preceded by a backslash (which will be included in the string), and it cannot end with a single backslash (or odd number of backslashes).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Einsteinium
  • 94
  • 13
0

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)
Chris Rudd
  • 709
  • 7
  • 13