f = open(\"C:\\Users\\aleed_000\\Desktop\\dis.txt\",\"r\")
print (f.read())
f.close()
Can anyone explain why this doesn\'t read from my file named
Use a raw string or escape each '\'
by prefixing another '\'
to it:
because C:\Users\aleed_000\Desktop\dis.txt
is actually interpreted like this:
>>> print ("C:\Users\aleed_000\Desktop\dis.txt") # '\a' gets escaped
C:\Usersleed_000\Desktop\dis.txt
Raw string:
>>> print (r"C:\Users\aleed_000\Desktop\dis.txt") #notice the r at the start
C:\Users\aleed_000\Desktop\dis.txt
Escape each '\'
:
>>> print ("C:\\Users\\aleed_000\\Desktop\\dis.txt")
C:\Users\aleed_000\Desktop\dis.txt