0

I wrote this simple little program:

def main ( ):
with open("test.txt", "rt") as fin:
    with open("out.txt", "wt") as fout:
            for line in fin:
                fout.write(line.replace("\", "/"))
print ("done")

main ()

I know that "\" is an escape literal in Python but all I need is to scan through the text file and replace every single backlash with a forward slash "/".

Anyone knows what to do?

teleton11
  • 11
  • 1
  • 3

1 Answers1

1

You have to remember that strings in python are interpreted. Only raw strings do not follow this rule. Here I mean that if for example you have a "\n" included in your string, it would be interpreted as a new line. Fortunately strings read from file are already raw.

All you have to do is simply use the regular expression:

s.replace('\\','/')
adgon92
  • 209
  • 1
  • 6