-1

When trying to make this replacement:

'C:\\Users\\uXXXXXX\\Downloads\\Folder\\Unprocessed\\FINAL_OUTBOUND.txt'.replace(r'\\', r'\')

Python throws the below error

File "<ipython-input-138-36d102855db9>", line 5
    'C:\\Users\\uXXXXXX\\Downloads\\Folder\\Unprocessed\\FINAL_OUTBOUND.txt'.replace(r'\\', r'\')
                                                                                                 ^
SyntaxError: EOL while scanning string literal

How can I make this replacement successfully?

Javi Torre
  • 724
  • 8
  • 23
  • 2
    You don't need to do that replacement at all - there are no occurrences of the string you're trying to replace in the original string. If you think there are, then you're misreading it. – user2357112 Mar 26 '21 at 10:15
  • Not sure what you mean. I'd like this: 'C:\Users\uXXXXXX\Downloads\Folder\Unprocessed\FINAL_OUTBOUND.txt' as the result. – Javi Torre Mar 26 '21 at 10:18
  • If you `print('C:\\Users\\uXXXXXX\\Downloads\\Folder\\Unprocessed\\FINAL_OUTBOUND.txt')`, you'll get the output you want. – khelwood Mar 26 '21 at 10:19
  • In python, you will use double backslash for single backslash so you are good. As @user2357112supportsMonica said, you dont need to do anything – Joe Ferndz Mar 26 '21 at 10:19

1 Answers1

0

What are you trying to replace? the double slash is because by default a backslash is an escape character, if you want it as a literal then you do a double backslash to escape it.

you can see when you print it, it doesnt print with double backslashes. if you dont want to put double backslash in the string then you can tell python to read it as a raw string and not consider any special meaning in chars. you do this by prefixing the string with an r

text = 'C:\\Users\\uXXXXXX\\Downloads\\Folder\\Unprocessed\\FINAL_OUTBOUND.txt'
print(text)

text = r'C:\Users\uXXXXXX\Downloads\Folder\Unprocessed\FINAL_OUTBOUND.txt'
print(text)

OUTPUT

C:\Users\uXXXXXX\Downloads\Folder\Unprocessed\FINAL_OUTBOUND.txt
C:\Users\uXXXXXX\Downloads\Folder\Unprocessed\FINAL_OUTBOUND.txt
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42