os.path.exists不适用于特定目录

时间:2017-01-16 16:00:27

标签: python windows python-3.x os.path

例如

os.path.exists

{{1}}不适用于此特定目录。为什么?我该怎么办?

2 个答案:

答案 0 :(得分:3)

'\r'表示回车。要从字面上使用\作为反斜杠,您需要将其转义:

filename = "C:\\Windows\\redir.txt"  # escape `\` s

或使用原始字符串文字:

filename = r"C:\Windows\redir.txt"  # raw string literal

答案 1 :(得分:2)

\r是回车符。您需要通过加倍\

来逃避它
filename = "C:\Windows\\redir.txt"
# Here ----------------^

或使用原始字符串作为前缀r

filename = r"C:\Windows\redir.txt"
# Here ----^