I have the following Python code:
localExtractpath = "D:\Python\From 0 to 1\Excel\"
if os.path.exists(localZipPath):
print("Cool! '" + localZipPath + "' exists...proceeding...")
This gives me the error:
File "", line 2
localExtractpath = "D:\Python\From 0 to 1\Excel\"
^
SyntaxError: EOL while scanning string literal
When I escape the last \
in the string, the code works. Why do I only have to escape the last \
?
The last backslash in "D:\Python\From 0 to 1\Excel\"
is escaping your ending quotation mark, so in the eyes of the interpreter, your string is unterminated. In fact, you have to escape all your backslashes if you want to use the literal backslash in your string:
"D:\\Python\\From 0 to 1\\Excel\\"