“扫描单引号字符串时 EOL”? (字符串中的反斜杠)

新手上路,请多包涵
import os
xp1 = "\Documents and Settings\"
xp2 = os.getenv("USERNAME")
print xp1+xp2

给我错误

 File "1.py", line 2
xp1 = "\Documents and Settings\"
                               ^
SyntaxError: EOL while scannning single-quoted string

你能帮帮我吗,你看到问题了吗?

原文由 Python 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 667
1 个回答

反斜杠字符被解释为转义符。对 Windows 路径使用双反斜杠:

 >>> xp1 = "\\Documents and Settings\\"
>>> xp1
'\\Documents and Settings\\'
>>> print xp1
\Documents and Settings\
>>>

原文由 gimel 发布,翻译遵循 CC BY-SA 2.5 许可协议

推荐问题