I have a file named 5_1.txt
in a directory named direct
, how can I read that file using read
?
I verified the path using :
For folks like me looking at the accepted answer, and not understanding why it's not working, you need to add quotes around your sub directory, in the green checked example,
x_file = open(os.path.join(direct, "5_1.txt"), "r")
should actually be
x_file = open(os.path.join('direct', "5_1.txt"), "r")
In case you're not in the specified directory (i.e. direct), you should use (in linux):
x_file = open('path/to/direct/filename.txt')
Note the quotes and the relative path to the directory.
This may be your problem, but you also don't have permission to access that file. Maybe you're trying to open it as another user.