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

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

最佳答案

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

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

10-08 12:43