Reputation: 134
For example
import os
filename = "C:\Windows\redir.txt"
if os.path.exists(filename):
print ("Y")
else:
print ("N")
os.path.exists
doesn't work for this particular directory. Why? What should I do?
Upvotes: 2
Views: 1499
Reputation: 369074
'\r'
represents Carriage Return. To use \
as a backslash literally, you need to escape it:
filename = "C:\\Windows\\redir.txt" # escape `\` s
or use raw string literal:
filename = r"C:\Windows\redir.txt" # raw string literal
Upvotes: 4
Reputation: 311348
\r
is the carriage-return character. You need to escape it either by doubling the \
:
filename = "C:\Windows\\redir.txt"
# Here ----------------^
or use a raw string by prefixing it with an r
:
filename = r"C:\Windows\redir.txt"
# Here ----^
Upvotes: 3