---
title: "Reading and Writing Files, Including CSV"
description: "Opening a file, reading it line by line or writing to it, and closing it again, plus the row-and-column shape of CSV. This is where a program's data starts outliving the run that produced it."
canonical: https://lightmysky.com/learn/computing/reading-and-writing-files-including-csv-mt_j5qb4FdkdI
source: https://lightmysky.com/learn/computing/reading-and-writing-files-including-csv-mt_j5qb4FdkdI.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Reading and Writing Files, Including CSV

Opening a file, reading it line by line or writing to it, and closing it again, plus the row-and-column shape of CSV. This is where a program's data starts outliving the run that produced it.

Subject: Computing · Area: Programming · Ages 17 to 18
Page: https://lightmysky.com/learn/computing/reading-and-writing-files-including-csv-mt_j5qb4FdkdI

## Ready when they can

- Read a text file line by line and build a result from it
- Write records to a CSV file and read them back into objects or dictionaries
- Say what closing a file does, and what a relative path depends on

## Lesson: Files that outlive your program

You open a file with open, which hands you a file object to work through. Then you pick how to pull text: read grabs the whole file as one string, readline grabs the next line, and readlines grabs every line as a list. When lines matter one by one, the line wise readers fit best.

**Example.** A line you read usually drags a newline character along, so you strip it before using the text. When you write lines you add the newline back yourself with write. CSV follows the same shape: rows and columns with commas splitting the columns, so one record per line.

You must close the file when done, because buffering can leave your words in memory instead of on disk. Skipping the close can leave a file empty or cut off with no error shown. Two more facts save confusion: a relative name like data.txt is resolved against where you run the program, often beside your script, and printing the file object shows its details, not its contents.

**Tip.** You build results from files line by line, then write records out to CSV for the next run. Variables die when the run ends, but the saved file waits, which is how your data outlives the program that made it.

**Recap.** Open a handle, read or write line by line, and close so the disk keeps it.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Designing with Objects: Deciding What Each Class Owns](https://lightmysky.com/learn/computing/designing-with-objects-deciding-what-each-class-owns-mt_De_gpvpOzg)

## Opens up

- [Exceptions: Raising, Catching and Recovering](https://lightmysky.com/learn/computing/exceptions-raising-catching-and-recovering-mt_hEVa1JRrGu)
- [Relational Databases: Tables, Keys and Relationships](https://lightmysky.com/learn/computing/relational-databases-tables-keys-and-relationships-mt_i7pZV6H902)
- [File Systems: Names, Blocks and Metadata](https://lightmysky.com/learn/computing/file-systems-names-blocks-and-metadata-mt_jN4u3idvF3)
- [Cleaning Real Data: Missing, Duplicated and Wrong](https://lightmysky.com/learn/computing/cleaning-real-data-missing-duplicated-and-wrong-mt_qIsLXA-7EC)
