Skip to content
← Python for AI/ML · all 50 levels

Files & with

Level 44 of 50 · Errors, files & modules

Level 44 of 50

Files & with

save your data with open(). Watch the reel, then work through it below: run the code, take the quiz, keep the cheat sheet.

📒 Open the cheat sheet
SanaAmmi's grocery list! I closed the program, and it's GONE!
TeacherSave it in a file! open, write, read. with!
list.py
with open("list.txt", "w") as f:
    f.write("Atta, Cheeni")

with open("list.txt") as f:
    print(f.read())
TeacherWrite the list into a file. Then open it again, and read it back!
SanaWhat is that little "w"?
Teacher"w" means write! No letter means read.
TeacherAnd with closes the file for you, automatically!

⏸ Pause & predict: what prints?

with open("d.txt", "w") as f:
    f.write("Ali")
with open("d.txt", "w") as f:
    f.write("Zara")
print(open("d.txt").read())
Next: JSON →← raise

Where this leads

Python is step one. Building AI that ships is the rest.

The diploma takes you from zero to deployed computer vision, LLM and agent projects, with engineers who build them for a living. Already code? The bootcamp does it in three months.