0

I need to compare paths. In a textile, I get paths like:

'C:\\\\Windows\\\\System32\\\\kernel32.dll'

The other path I get from the command line.

To compare the two strings I tried:

while path.find('\\') != -1:
    path.replace('\\\\','\\', 1)

but this changes nothing. Also the builtin functions os.path.normpath() and os.path.realpath() don't remove the backslashes. How to remove the backslashes from the string?

Georgy
  • 12,464
  • 7
  • 65
  • 73
spitzbuaamy
  • 751
  • 2
  • 10
  • 25
  • 1
    path = path.replace('\\\\','\\', 1) – Promination May 11 '17 at 13:29
  • did you try ? http://stackoverflow.com/questions/3160752/removing-backslashes-from-a-string-in-python – Sohan May 11 '17 at 13:30
  • Does this answer your question? [Why doesn't calling a Python string method do anything unless you assign its output?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – Georgy Oct 24 '20 at 10:34

4 Answers4

1

In C# you need to assign the return value from replace. Something like that:

path = path.replace('\\\\','\\', 1)

but I think you are using Java and I don't know if is the same, but try it

RMH
  • 222
  • 1
  • 11
1

to replace 2 \ by one \ , you can do like that:

value = "C:\\\\Windows\\\\System32\\\\kernel32.dll"
print value.replace("\\\\", "\\")

gives me:

C:\Windows\System32\kernel32.dll
Cœur
  • 37,241
  • 25
  • 195
  • 267
1

Try:

path = path.replace('\\\\','\\', 1)
Ludisposed
  • 1,709
  • 4
  • 18
  • 38
1

This returns a single backslash and can be compared to output from os.getcwd()

path = path.replace('\\\\', '\\')
misantroop
  • 2,276
  • 1
  • 16
  • 24