Reputation: 582
So I'm having problems opening a file for reading, and so I decided to just try an os.isfile assert:
from Android_API_Parser import Android_API_Parser
import os.path
assert os.path.isfile("D:\Work\Python Workspace\Android_API_Parser\test.txt")
tester = Android_API_Parser()
tester.setFile("test.txt")
tester.parse()
It's failing the assert:
Traceback (most recent call last):
File "D:\Work\Python Workspace\Android_API_Parser\src\Android_API_Tester.py", line
9, in <module>
assert os.path.isfile("D:\Work\Python Workspace\Android_API_Parser\test.txt")
AssertionError
I have opened the path to the file I'm trying to open and pasted it below:
D:\Work\Python Workspace\Android_API_Parser\test.txt
Any ideas as to why it's even failing the assert? Unless I'm really tired, the file is clearly located there. I even tried both with "/" and "\", even with escape characters included.
Upvotes: 0
Views: 15741
Reputation: 287865
In a string literal, you must escape backslashes with another backslash, use a raw string, or use forward slashes. Otherwise, "\t"
becomes a string that just contains the tab character.
Try any of:
assert os.path.isfile("D:\\Work\\Python Workspace\\Android_API_Parser\\test.txt")
assert os.path.isfile(r"D:\Work\Python Workspace\Android_API_Parser\test.txt")
assert os.path.isfile("D:/Work/Python Workspace/Android_API_Parser/test.txt")
assert os.path.isfile(os.path.join("D:", "Work", "Python Workspace",
"Android_API_Parser", "test.txt"))
The file may also not be a regular file. Use os.path.exists
to see if it exists.
You may also have insufficient privileges to see the file, or the file name you expect may be localized. To debug this, run:
path = ["Work", "Python Workspace", "Android_API_Parser", "test.txt"]
f = 'D:'
for p in path:
f = os.path.join(f, p)
print(f)
assert os.path.exists(f)
assert os.path.isfile(f)
Upvotes: 4