Ask coding questions

← Back to all posts
beginner trying to ask about python
HorikitaSuzune (0)

Hello im new to python and in this online ide. Can i ask how to code the "how to read a text file in python" in this online ide??

Answered by 19ecal (228) [earned 5 cycles]
View Answer
Comments
hotnewtop
19ecal (228)

Reading text files is simple
First, create a text file.
Click on the add file button.

Type a file name, then .txt and hit enter
Inside the file, add whatever you want
Then go back to your main.py and put this code

#Create a file object (replace <your-file-name> with your file name)
f=open("<your-file-name>.txt")
#Read it
data=f.read()
#Output the contents
print(data)
#Close the file
f.close()

That should output whatever you put inside your text file

HorikitaSuzune (0)

thank you so much XD