q5226436 发表于 2020-4-27 00:37:08

请问这个代码错在哪里

>>>str = r'C:\Program Files\Go Man\'
SyntaxError: EOL while scanning string literal


>>>str = r'C:\Program Files\Go Man\\'
就没问题,
print(str)之后可以输出
C:\Program Files\Go Man\\ ,最后双反斜杠

sunrise085 发表于 2020-4-27 00:47:20

本帖最后由 sunrise085 于 2020-4-27 08:35 编辑

字符串不能以反斜杠'\'结尾。
若真的需要的话,有两种方式
str = r'C:\Program Files\Go Man\\'[:-1]

str = r'C:\Program Files\Go Man'+"\\"

xiangjianshinan 发表于 2020-4-27 00:56:31

建议:
str ='C:/Program Files/Go Man/'

ouyunfu 发表于 2020-4-27 03:28:30

SyntaxError- EOL while scanning string literal
An EOL ( End of Line ) error indicates that the Python interpreter expected a particular character or set of characters to have occurred in a specific line of code, but that those characters were not found before the end of the line . This results in Python stopping the program execution and throwing a syntax error .

建议用\str =r'C:/Program Files/Go Man/'

永恒的蓝色梦想 发表于 2020-4-27 07:20:03

建议直接input,复制结果

zltzlt 发表于 2020-4-27 08:06:30

sunrise085 发表于 2020-4-27 00:47
字符串不能以反斜杠'\'结尾。
若真的需要的话,有两种方式

str = r'C:\Program Files\Go Man'+"\\"

会报错

liuzhengyuan 发表于 2020-4-27 08:11:41

zltzlt 发表于 2020-4-27 08:06
会报错

是不是论坛又把反斜杠吞掉了{:10_327:}

zltzlt 发表于 2020-4-27 08:11:53

原始字符串不能以反斜杠结尾,如果需要可以这样灵活处理:

>>> str = r'C:\Program Files\Go Man''\\'    # 相当于 r'C:\Program Files\Go Man' + '\\'
>>> print(str)
C:\Program Files\Go Man\

zltzlt 发表于 2020-4-27 08:12:12

liuzhengyuan 发表于 2020-4-27 08:11
是不是论坛又把反斜杠吞掉了

是的

txxcat 发表于 2020-4-27 08:22:47

或者:
str ='C:\\Program Files\\Go Man\\'

sunrise085 发表于 2020-4-27 08:35:53

zltzlt 发表于 2020-4-27 08:06
会报错

呃,是编辑问题,本来是俩反斜杠的。放到代码中,就变成一个了。修改了
页: [1]
查看完整版本: 请问这个代码错在哪里