← Courses

Python for AI — Beginner Foundations

Data Science

Practical Python · Lesson 59 of 76

Working with files

Programs often need to save results and read them back. Python opens files with open(), and the safe way is a with block, which closes the file for you.

This actually runs here — the browser engine has its own small disk:

Python
  • "w" means write (it replaces the file). "a" would add to the end.
  • No mode given means read.
  • .strip() removes the newline at the end of each line.

Your turn: add a fourth name to the file and read it back.

You’re reading for free. Sign in to keep your progress and earn a certificate when you finish.Sign in to keep my progress →