-1

For example I want to take input the absolute path from the user like "C:\Users\User\Desktop" and change it to "C:\\Users\\User\\Desktop" but when I do the following:

path = input("Enter path: ") and path = path.replace("\", "\\")

it throws syntax error as the escape sequence of \\ is triggered. How do I bypass this and achieve the replace?

Correction: It takes input and adjusts itself! There is no need to do such a thing as mentioned above...

Broteen Das
  • 386
  • 4
  • 12

2 Answers2

5

Backslash is a control character in Python, meaning that a single backslash in your Python script does not, by itself, mean a single literal backslash. You may escape it to make this work:

path = path.replace("\\", "\\\\")

In this case, \\ represents a single literal backslash, and \\\\, the replacement, represents two literal backslahses.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-1

You can use

os.path.join() or 
path=input()
path='//'.join(path.split('/'))
NAGA RAJ S
  • 452
  • 4
  • 12