Lesson 30 of 34
Files: Writing and Reading
9 min 30 XP
To keep data after a program ends, save it to a file. open(name, "w") writes, "a" appends and "r" reads. Using with closes the file for you.
Example: edit me and press Run
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.Loop over a file to read it line by line. strip() removes the \n at the end of each line.
Example: edit me and press Run
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.Here, files live only inside your browser and vanish on refresh. With Python installed on your computer, real files are created.