0

I have a code where I try to get a path from an input so this program could append info to another text file.

file1 = input("Input the file path:\n")
file2 = file1.replace("\", "/")
file2 = file1.replace("\", "/")
                              ^
SyntaxError: EOL while scanning string literal

I get this problem and it seems to be because the \ is not seen as a string (if I put \n for example it does work but I need to replace )

An alternative for replacing \ with / could also fix my problem. I used to use r"filepath" but I can't do that with an input I think. I just need to make the input into a raw string.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • This has nothing to do with `.replace`. A string literal can not end with a backslash because it escapes the closing `'` or `"`. You need to escape the backslash with a second backslash (`'\\'`) – DeepSpace Feb 04 '21 at 23:13
  • `input()` already returns a "raw string". You should use `os.path` or `Pathlib` to deal with fllepath creation rather than replacing+appending strings – OneCricketeer Feb 04 '21 at 23:24

0 Answers0