how to remove or replace \ a text string

0

Good afternoon I have a string that is a windows path c: \ user \ x but I want to delete the lines or change them "\" however it marks me an error when trying to do it this is my code:

 string = "C:\User\New\Desktop"
 s_replace = string.replace("\"," ")

but I did not work

    
asked by Mystic_Force 23.08.2018 в 01:40
source

3 answers

0

If you want to delete them use s_replace = string.replace("\","")

    
answered by 23.08.2018 / 06:25
source
0

I found the solution:

import os

print('\')
for r, d, f in os.walk("C:\"):
    for files in f:
        if files == "WINWORD.EXE":

            print(os.path.join(r,files))
            r = os.path.join(r,files)
            res = r.replace('\','\\') #escapando \ 2 veces obtengo el resultado deseaso
            print(res)

            break

** Result: **

\
C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE
C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE
    
answered by 23.08.2018 в 05:48
0

What I'm saying is that to eliminate the character, you must put the quotes without a space, that is: s_replace = string.replace("\", "") and not s_replace = string.replace("\", " ") , since when you put a space between the quotes you are trying to change "\" " " , the other thing I suggest you do before removing the character is that you do this: string = str(string) if the format you have is None

    
answered by 23.08.2018 в 16:22