✦ Python Foundations Working with Data module

Concept 20 · Working with Files

Save and read text.
Use a temporary browser file.

Most values disappear when a program finishes. A text file lets a program save text, then read that text again during a later step.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Open a text file, use it, then let Python close it.

A variable keeps a value only while the program is running. A text file can keep characters outside that temporary program state. To work with one safely, open it, do one clear task, and leave the with block so Python closes it for you.

with open("notes.txt", "w", encoding="utf-8") as file:
    file.write("Learning Python is fun!")

with open("notes.txt", "r", encoding="utf-8") as file:
    content = file.read()

print(content)

open("notes.txt", "w", encoding="utf-8") opens a file named notes.txt. The "w" means “write”; encoding="utf-8" tells Python how to save and read ordinary text. as file gives the open file a temporary name for the indented block below it.

Learning Python is fun!

with ... as file: is a safe pattern, not a new kind of value you need to memorize. Inside its indented block, use file to write or read. When execution leaves that block, Python closes the file automatically, even though the program never calls a separate close command here.

↳

Keep this file life cycle in mind: program → open file → write or read → leave with block → close automatically

Trace one complete write-and-read cycle
with open("notes.txt", "w", encoding="utf-8") as file:
    file.write("Learning Python is fun!")

with open("notes.txt", "r", encoding="utf-8") as file:
    content = file.read()

print(content)

First Python opens notes.txt in write mode and puts the string into the file. Leaving that first with block closes it. Then Python opens the same file in read mode, file.read() returns its contents as a string, and content stores that returned string. Finally, print(content) displays it after the second block closes.

Read the arguments to open() one at a time
open("notes.txt", "w", encoding="utf-8")

"notes.txt" is the file name. "w" opens it for writing, and "r" opens it for reading. UTF-8 is a common way to represent text, so using the same encoding when writing and reading helps Python interpret the characters consistently.

file.write() sends a string into the file
file.write("Learning Python is fun!")

file.write() takes a string and places those characters in the open file. It does not print the text to the screen; the text becomes the file content.

Write mode starts fresh
with open("notes.txt", "w", encoding="utf-8") as file:
    file.write("New note")

Opening an existing text file with "w" replaces its previous content. That is useful when the program should create a fresh saved version, but it means you should choose write mode carefully.

file.read() brings file content back as a string
content = file.read()

file.read() reads the characters from a file opened with "r" and returns one string. Assigning that result to content lets later code print it, inspect it, or work with it as text.

The name after as is temporary
with open("notes.txt", "r", encoding="utf-8") as file:
    content = file.read()

file is simply the name this example uses while the file is open. Use it inside the indented block, then keep the string result, such as content, after the block has finished.

The browser playground uses a temporary file environment
with open("notes.txt", "w", encoding="utf-8") as file:
    file.write("Practice")

The playground can demonstrate file code safely, but its files live in a temporary browser Python environment. They are not saved as files on your computer after the playground session ends.

A practical file-reading checklist

  1. Find the file nameWhich text names the file that this block will open?
  2. Find the modeIs the file opened with "w" to write or "r" to read?
  3. Read the with blockWhich indented lines run while the temporary file name is available?
  4. Follow the data directionDoes a string move into the file with write(), or out of it with read()?
  5. Notice when the block endsAfter the indentation ends, Python closes that open file automatically.
  6. Find the usable resultAfter reading, which string variable holds the file content for later code?

Common beginner mistakes

  • Using read mode when the program needs to writeA file opened with "r" is for reading. Use "w" when the next step is file.write(...).
  • Forgetting that write mode replaces old contentOpening the same existing file with "w" starts fresh, so previous text is replaced.
  • Trying to read from a write-only blockOpen the file again with "r" before using file.read().
  • Trying to write in a read-only blockUse a separate "w" block before the read block when the program needs to save text first.
  • Putting file work outside the indentationThe statements that use file belong inside the indented with block.
  • Using file after its with block has endedWhen the block ends, that open file is closed. Keep a returned string such as content for later use instead.

Python Playground

Loading Python runtime…

This browser playground runs file code in a temporary environment. Change the text, run the complete write-and-read example again, and watch the printed string. It does not create a file on your computer.

working_with_files.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each data operation before choosing an answer.

with open("todo.txt", "w", encoding="utf-8") as file:
    file.write("Read\nPractice")

with open("todo.txt", "r", encoding="utf-8") as file:
    print(file.read())
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.