Python在扫描字符串li时,试图以“\”结束文件地址,导致EOL

2024-05-13 18:10:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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")`

文件地址所需的反斜杠导致代码忽略引号,在扫描字符串文本错误时返回EOL。我不知道怎么解决这个问题。在


Tags: pathimporttxttrueinputifosexists
1条回答
网友
1楼 · 发布于 2024-05-13 18:10:12

\是字符串文本中的转义字符。它修改随后角色的行为。在您的例子中,它修改了"的含义,不再表示字符串的结尾。在

尝试以下几种方法之一:

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

相关问题 更多 >