繁体   English   中英

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

[英]os.path.exists is not working for particular directory

例如

 import os
filename = "C:\Windows\redir.txt"
if os.path.exists(filename):
    print ("Y")
else:
    print ("N")

os.path.exists不适用于此特定目录。 为什么? 我该怎么办?

'\\r'代表回车。 要将\\用作反斜杠,您需要对其进行转义:

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

或使用原始字符串文字:

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

\\r是回车符。 您需要通过加\\来转义它:

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

或在原始字符串前加上r作为前缀:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM