Working with files and paths

18-minute lesson · module-files-01

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Working with files and paths

18-minute lesson · module-files-01

  • Open a file with `open(path, mode)` and close it automatically with `with`.
  • Choose the right mode among read, write, and append.
  • Write text to a file and confirm it landed by reading it back.
  • Explain why the with-statement is safer than calling `open` and `close` by hand.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Files / Working with Data Files / Finding a File on your Disk

Adaptation notes. Reading rewritten in our voice. The roster example is ours. The coding exercise uses a path argument and a /tmp file so it runs identically in the Pyodide sandbox and in a local python subprocess: the test writes the file through the student's function, then reads it back to verify.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.