I1 · Python Programming for AI

Reading and Writing Files

A file stores data on disk so that it outlives the program. open() opens a file in a mode: "r" to read, "w" to write (replacing what was there) and "a" to append. The with statement closes the file automatically. The csv module reads and writes comma-separated files, the usual format for tables of data.

10 min Intermediate · Sublevel Beginner Jump to the activity

What you need: The earlier topics of this module.

Definition

How does a program remember data after it closes?

A file stores data on disk so that it outlives the program. open() opens a file in a mode: "r" to read, "w" to write (replacing what was there) and "a" to append. The with statement closes the file automatically. The csv module reads and writes comma-separated files, the usual format for tables of data.

In simple words

Variables vanish when a program ends. Writing to a file is like writing in a notebook you can open tomorrow. with open("notes.txt", "w") as f: f.write("Hello") saves it; with open("notes.txt") as f: f.read() brings it back. A CSV file is a spreadsheet saved as plain text.

Overview

  • Files let a program keep data after it stops and read data that others created.
  • open(path, mode) returns a file object; with open(...) as f: closes it automatically.
  • read() reads everything, readline() one line, and looping over the file reads line by line.
  • write() and writelines() store strings; numbers must be converted with str() first.
  • Text files should be opened with encoding="utf-8" so that Hindi and other scripts are stored correctly.
  • Paths can be relative (data/marks.txt) or absolute; pathlib.Path makes them easy to build.

Why it matters

Almost every AI project starts by reading data from files, usually CSV, and ends by saving results, models or reports. Doing it safely, with with and the right mode, prevents lost data.

Key terms

File mode
How a file is opened: "r" read, "w" write (replace), "a" append, "b" binary.
Context manager
The with statement, which closes a file automatically even if an error occurs.
Encoding
The rule for turning text into bytes; UTF-8 handles every language.
CSV
Comma-separated values: a plain-text table, one row per line.
Path
The location of a file, absolute from the drive root or relative to the current folder.
Persistence
Data outliving the program that created it.

Types

Files are opened in different modes, and data is stored in different formats. These are the types you will use.

1. Read mode ("r")

Opening an existing file only to read it; this is the default mode.

  • How it works: The file must exist, or FileNotFoundError is raised; the position starts at the beginning.
  • Example: with open("marks.txt", encoding="utf-8") as f: lines = f.readlines().
  • When to use it: Use it to load input data, settings or saved results.

2. Write mode ("w")

Opening a file to write, creating it if needed and erasing it if it already exists.

  • How it works: Everything previously in the file is lost the moment it is opened in "w" mode.
  • Example: Saving a fresh report: with open("report.txt", "w") as f: f.write("Average: 78\n").
  • When to use it: Use it to create new output files; double-check the path so you do not overwrite important data.

3. Append mode ("a")

Opening a file to add to its end, keeping what is already there.

  • How it works: The write position starts at the end; the file is created if missing.
  • Example: Adding one line to a daily log: with open("log.txt", "a") as f: f.write(entry + "\n").
  • When to use it: Use it for logs and records that grow over time.

4. Exclusive create ("x") and read-write ("r+")

"x" creates a new file but fails if it exists; "r+" opens an existing file for both reading and writing.

  • How it works: "x" raises FileExistsError instead of overwriting; "r+" starts at the beginning and overwrites characters where you write.
  • Example: open("results_2024.csv", "x") protects last year's results from being replaced.
  • When to use it: Use "x" to avoid accidental overwrites; use "r+" rarely, for small in-place edits.

5. Binary mode ("rb", "wb")

Reading or writing raw bytes instead of text.

  • How it works: Adding b to the mode skips text encoding, so images, audio, PDFs and saved models are copied exactly.
  • Example: with open("photo.jpg", "rb") as f: data = f.read() reads the image's bytes.
  • When to use it: Use it for any non-text file; never open an image in text mode.

6. Structured formats: CSV and JSON

Text files with a standard layout: CSV for tables, JSON for nested records.

  • How it works: The csv module reads rows as lists or dictionaries; the json module turns JSON text into dictionaries and lists and back with json.load and json.dump.
  • Example: Reading students.csv into rows; saving a model's settings to config.json.
  • When to use it: Use CSV for spreadsheets and data tables (later with pandas), and JSON for settings and web API data.

Explained in detail

Opening files safely

Variables vanish when a program ends; files persist. The safe way to use a file is with open("marks.txt", encoding="utf-8") as f:. The with block guarantees the file is closed when the block ends, even if an error happens inside, which prevents lost data and locked files. Always pass encoding="utf-8" when working with text. Without it, Python uses the operating system's default, which on Windows may not handle Hindi or other scripts, producing the infamous UnicodeDecodeError.

Reading and writing

CodeDoesCareful
f.read()The whole file as one stringUses lots of memory for huge files
for line in f:One line at a timeEach line ends with \n; use line.strip()
f.readlines()A list of all linesSame memory concern as read()
open(..., "w")Creates or empties the file, then writesDestroys existing content without warning
open(..., "a")Adds to the endGood for logs
f.write(text)Writes a stringDoes not add a new line; add "\n" yourself

CSV files

Most data sets you will meet in AI start life as CSV files: a header line of column names, then one record per line, values separated by commas. Splitting lines on commas by hand breaks as soon as a value contains a comma, such as "Mumbai, Maharashtra". The csv module handles quoting correctly. csv.DictReader(f) gives each row as a dictionary keyed by the header, so row["maths"] reads naturally. Remember that every value arrives as a string; convert numbers with int() or float(). Open CSV files with newline="" when writing, as the documentation recommends, to avoid blank lines on Windows.

Paths and other formats

  • pathlib.Path("data") / "marks.csv" builds paths that work on Windows, macOS and Linux.
  • Path("marks.csv").exists() checks a file is there before opening it.
  • Relative paths depend on the folder you run from; a FileNotFoundError often means you are in the wrong folder.
  • json.dump() and json.load() save and load dictionaries and lists, ideal for settings and API results.
  • Images, audio and trained models are binary files, opened with "rb" or "wb", or through libraries.

Later, pandas will read a whole CSV into a table in one line with pd.read_csv(). Understanding what happens underneath, lines, separators, headers and strings, will help you fix the messy files that real projects always contain.

How it works

ModeMeaningIf the file exists
"r"Read (the default)Read it
"w"WriteErase it and start fresh
"a"AppendAdd to the end

In code

Saving an attendance register as a CSV file, reading it back, and appending a line to a log.

attendance.py
import csv

register = [["name", "present"], ["Aarav", "yes"], ["Meera", "no"], ["Kabir", "yes"]]
with open("attendance.csv", "w", newline="") as f:
    csv.writer(f).writerows(register)

with open("attendance.csv") as f:
    rows = list(csv.DictReader(f))
present = [row["name"] for row in rows if row["present"] == "yes"]
print("Rows read:", len(rows))
print("Present:", present)

with open("log.txt", "w") as f:
    f.write("Register saved")
with open("log.txt", "a") as f:
    f.write(" and checked")
with open("log.txt") as f:
    print("Log says:", f.read())

Output

Rows read: 3
Present: ['Aarav', 'Kabir']
Log says: Register saved and checked

Try it yourself

Which mode?

Match each job to the file mode it needs.

  • Load yesterday's saved scores
  • Start a brand-new report, replacing the old one
  • Add today's entry to the end of a diary

Common mistakes

  • Opening with "w" when you meant "a", and losing everything.
  • Forgetting to close a file. Use with and it's done for you.
  • Treating CSV numbers as numbers. They are read as text; convert with int() or float().

Check yourself

Question 1

1. Which mode adds to the end of a file?

Question 2

2. Why use with open(...)?

Question 3

3. Values read from a CSV file arrive as…

Summary

  • Files keep data after the program ends.
  • Modes: r reads, w replaces, a appends; with closes the file.
  • csv reads and writes tables; convert text to numbers yourself.

Finished reading?

Sources: Python docs: Reading and Writing Files