Skip to content
← Back to Community
python open and read file with repl.it
Profile icon
AbdelghaniTahar

how to open and read file with repl.it on line

Voters
Profile icon
CK2020
Profile icon
18FMartin
Profile icon
JikanTribal
Profile icon
AnineStener
Profile icon
AbdelghaniTahar
Comments
hotnewtop
Profile icon
CarolineML

It doesn't work for me. I don't know why. I enter this (after having uploaded my file in replit):
f = open(test.txt, "r")
I have this error code:
raceback (most recent call last):
File "main.py", line 2, in
f = open(test.txt, "r")
NameError: name 'test' is not defined
I tried with several files types (xlsx, csv...)
Do you know what I do wrong ?

Profile icon
HappyFakeboulde

Add a file in the editor
Give it a name
In your program put f = open(filename, "r") where filename is the name of the file you created
To read from the file put t = f.read() where t is the name of the variable to save it in
Remember to close the file object with f.close()

Profile icon
Hwiggy