spitzbuaamy
spitzbuaamy

Reputation: 771

Using str.replace method changes nothing in the original string

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?

Upvotes: 0

Views: 2611

Answers (4)

Laurent Grousset
Laurent Grousset

Reputation: 403

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

Upvotes: 1

misantroop
misantroop

Reputation: 2554

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

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

Upvotes: 1

Ludisposed
Ludisposed

Reputation: 1769

Try:

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

Upvotes: 1

RMH
RMH

Reputation: 222

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

Upvotes: 1

Related Questions