Skip to content

File Input and Output

By the end of this lesson you'll be able to:

  • Open a file for reading or writing using open() and the with statement
  • Read file contents with read(), readlines(), and line iteration
  • Write and append text to a file with write()
  • Read and write simple CSV files using Python's csv module

Programs become much more useful when they can save data between runs. With file I/O, your programs can remember high scores, process text files, and share data with other programs.

Welcome to Chapter 26!

Monty waving welcome Today your programs stop forgetting everything when they close! Files let your programs save and load data — a superpower that opens up a whole new world of projects. Let's code it together!

File I/O Needs a Real Python Environment

Monty with a warning Actual file operations — open(), write(), read() — access your computer's disk. The Skulpt browser environment cannot do this, so the interactive labs below simulate file behavior using Python strings and lists. To run the real open()/with code shown in the code blocks, use Thonny, VS Code, or any local Python installation.

The Command Line and File System Navigation

Files live in a file system organized as folders inside folders. When you run a Python script from the terminal, Python looks for files relative to the current working directory — the folder the script lives in.

1
2
3
4
cd foldername    # move into a folder
cd ..            # move up one level
ls               # list files (Mac/Linux)
dir              # list files (Windows)

open() and File Modes

open(filename, mode) opens a file and returns a file object.

The mode tells Python what you want to do with the file:

Mode Meaning
"r" Read (default) — file must exist
"w" Write — creates file or overwrites existing content
"a" Append — adds to the end without deleting existing content
"b" Binary — used with "rb" or "wb" for images and other binary files

The with Statement — Safe File Handling

The with statement opens a file, runs the indented block, then automatically closes the file — even if an error occurs.

1
2
3
with open("greeting.txt", "w") as f:
    f.write("Hello from Python!\n")
# file is automatically closed here

Always use with open(...). It prevents the most common file bug: forgetting to close the file.

Writing to a File

file.write(text) writes text to the file. It does not add a newline automatically — you must add "\n" yourself.

Real Python (Thonny / VS Code):

1
2
3
4
with open("notes.txt", "w") as f:
    f.write("Line 1: Hello, Python!\n")
    f.write("Line 2: I am writing to a file.\n")
    f.write("Line 3: This is fun!\n")

Skulpt simulation — using a list as a virtual "file":

What Do You Think Will Happen?

Monty thinking The code below uses a list to simulate writing to a file and then reading it back. In what order will the lines appear when we read them? Make your guess — then run it!


  

Reading from a File

Three methods read file content:

  • f.read() — reads the entire file as one big string
  • f.readlines() — reads all lines into a list (each line includes \n)
  • f.readline() — reads one line at a time

Real Python:

1
2
3
with open("notes.txt") as f:
    content = f.read()
    print(content)

Skulpt simulation — splitting a multi-line string:


  

Note the .strip() call — it removes whitespace and \n from the ends of lines.

Iterating over File Lines

The cleanest way to process a file line by line is a simple for loop:

1
2
3
4
5
with open("data.txt") as f:
    for line in f:
        line = line.strip()
        if line:   # skip empty lines
            print(line)

Text Processing with strip() and lower()

When processing file content, always clean the text first:

  • .strip() — removes leading and trailing whitespace
  • .lower() — converts to lowercase for case-insensitive comparisons
  • .split() — splits a line into words

  

Appending to a File

Mode "a" adds to the end of a file without erasing existing content:

1
2
with open("log.txt", "a") as f:
    f.write("New entry added.\n")

Run this multiple times and the log grows — each run adds a new line.

CSV Files

CSV (Comma-Separated Values) is one of the most common file formats for tabular data. Each row is one line; columns are separated by commas.

1
2
3
4
name,age,score
Alice,14,95
Bob,13,87
Carol,15,91

Python's csv module reads and writes CSV files cleanly. csv.DictReader takes any iterable of strings — so we can pass csv_text.splitlines() directly, no file object needed.

splitlines() splits a multi-line string into a list of lines — exactly what csv.DictReader needs.


  

csv.DictReader uses the first row (the header) as column names and gives you each row as a dictionary.

Counting Word Frequencies — A Mini Case Study

Reading text becomes powerful once you start asking questions about it. A classic exercise is counting how many times each word appears in a piece of text — the same basic idea search engines and spam filters use.

Real code would read the text from a file with open(); here we simulate a short excerpt with a string so it runs right in the browser. Punctuation gets stripped one character at a time before splitting the text into words:

What Do You Think Will Happen?

Monty thinking The code below counts word frequencies in a short excerpt. Which word do you think will appear the most times? Make your guess — then run it!


  

Were you right? Short, common words like "the" and "i" repeat the most in this excerpt — with a whole book of text, that pattern becomes even more dramatic.

Chapter 29 shows a faster way to strip punctuation and split text using regular expressions.

Learning Check

Your Turn — Process the CSV

Monty thinking The code below reads student scores from a CSV string. Add code to calculate and print the average score after the loop. You'll need to convert the score strings to integers first!


  

Experiments

Try these changes. Predict what will happen first, then run it to check!

  1. Change the CSV to add a fourth column "grade" with values A/B/C. Print only rows where grade == "A". You'll know it worked when only Alice's row appears.

  2. Use virtual_file (a list) to simulate a to-do list: "write" three tasks, then print each one numbered 1, 2, 3. You'll know it worked when each task prints on its own numbered line.

  3. Count how many words appear in a multi-line text string using split() and print the total. You'll know it worked when you see the correct number of words.

  4. Read lines from a string and filter out any line that contains the word "skip". You'll know it worked when lines containing "skip" don't appear in the output.

  5. In Thonny or VS Code, create a file called fruits.txt, write five fruit names (one per line), then open it again and print each line with its line number. You'll know it worked when all five fruits print in a real Python terminal with their numbers.

File I/O Expert!

Monty celebrating Your programs can now save and load data from files — including CSV spreadsheets! This opens the door to real data processing, log files, configuration storage, and so much more. For the real thing, open Thonny and practice open() on your own computer. Let's keep coding!

Take the Chapter Review Quiz

See Annotated References