我有以下Python代码:
localExtractpath = "D:\Python\From 0 to 1\Excel\"
if os.path.exists(localZipPath):
print("Cool! '" + localZipPath + "' exists...proceeding...")
这给了我错误:
File "", line 2
localExtractpath = "D:\Python\From 0 to 1\Excel\"
^
SyntaxError: EOL while scanning string literal
当我转义\
字符串中的最后一个时,代码起作用。为什么我只需要逃避最后一个\
?
最后的反斜杠"D:\Python\From 0 to 1\Excel\"
转义了您的结束引号,因此在解释器的眼中,您的字符串未终止。实际上,如果要在字符串中使用文字反斜杠,则必须转义所有反斜杠:
"D:\\Python\\From 0 to 1\\Excel\\"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句