Search code examples
pythoneol

Python - Attempting to end file adress with a \", causes EOL while scanning string literal


import os.path
LicencePlate = input("Plate me Bitch")
savePath = "M:\Python\PlatesOffences\"
print(savePath + LicencePlate)
if os.path.exists(savePath + + LicencePlate + ".txt" ) == True:
    print("FileFound")`

The backslash that I need for the file address causes the code to ignore the Quote mark, returning an EOL while scanning string literal error. I have no ide how to fix this.


Solution

  • The \ is an escape character in string literals. It modifies the behavior of the character that follows. In your case, it modified the meaning of " to no longer represent the end of the string.

    Try one of these alternatives:

    savePath = "M:\\Python\\PlatesOffences\\"
    savePath = "M:/Python/PlatesOffences/"