Skip to content
Sign upLog in
← Back to Community
how to read a text file in a directory in python
Profile icon
JonahElias

I've been working on a project with a lot of different files.

It would be a lot more organized if I could put files into directories based on their categories.

Unfortunately, when I try to read a file from a directory, it says "no such file or directory named ie.txt"

Does anyone know how to read a text file from a directory?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
kamleshmj07
Profile icon
JonahElias
Comments
hotnewtop
Profile icon
Coder100
file = open("path/to/file.txt", "r") print(file.read())

Easy, right?
And if your directory was:

path/ to/ file.txt

it would read perfectly.

Now, what if the files looked like this:

main.txt folder/ main.py

you can read main.txt:

file = open("../main.txt") print(file.read())

Also super easy :D

Profile icon
JonahElias

@Coder100
apparently

Profile icon
Coder100