Skip to content
Skip to page content
soylai.com

File Handling — notes

6 sections 5 min readBeta

1. Files, kinds of file and paths

A file keeps data on disk after a program ends (data in variables are lost).

  • Text file: characters in lines; each line ends with the EOL character \n. Readable in any editor.
  • Binary file: raw bytes, e.g. Python objects stored with pickle; not human-readable.
  • CSV file: a text file of comma-separated values, one record per line; opens in spreadsheets.

Paths. An absolute path starts at the root (C:\School\cs\a.txt, /home/asha/cs/a.txt). A relative path starts at the current working directory (a.txt, data\a.txt, ..\a.txt, where .. is the parent folder).

Table of the three kinds of file: text files store characters in lines, binary files store raw bytes such as pickled objects, and CSV files store comma-separated values; each with a typical use and how it is read
KindWhat it storesTypical useRead with
TextCharacters in lines ending with \nNotices, logs, storiesread(), readline(), readlines()
BinaryRaw bytes, e.g. pickled Python objectsRecords kept as lists or dictionariespickle.load()
CSVText lines of comma-separated valuesTables shared with spreadsheetscsv.reader()
In a Python string write Windows paths as r"C:\School" or with doubled backslashes.
PathKindMeaning
C:\School\CS\notes.txtAbsolute (Windows)Full location from the drive root
/home/asha/cs/notes.txtAbsolute (Linux)Full location from the root /
notes.txtRelativeIn the current working directory
data\notes.txtRelativeIn the data folder inside the current one
..\notes.txtRelativeIn the parent of the current folder

Key terms

EOL

End-of-line character (\n) that ends each line of a text file.

Absolute path

The full location of a file from the root of the drive.

Relative path

A location worked out from the current working directory.

2. Opening and closing files

f = open(name, mode) returns a file object; f.close() saves and releases the file.

  • "r" read (default; file must exist), "w" write (creates or empties), "a" append (creates or adds at the end).
  • "r+", "w+", "a+" add the other operation; add b for binary: "rb", "wb", "ab".

The with statement closes the file automatically, even after an error:

with open("notes.txt", "a") as f:
    f.write("New entry\n")
Table of file modes r, w, a, r plus, w plus and a plus, with what each opens for, what happens if the file exists or not, and where the file pointer starts; adding b gives the binary versions
ModeOpens forIf the file existsIf it does not existPointer starts at
rReading (default)Read from the startFileNotFoundErrorBeginning
wWritingEmptied (truncated)CreatedBeginning
aAppendingKept; writes go at the endCreatedEnd
r+Reading and writingKeptFileNotFoundErrorBeginning
w+Writing and readingEmptiedCreatedBeginning
a+Appending and readingKeptCreatedEnd
rb wb ab …The same, in binaryAs aboveAs aboveAs above
A with block does the close() for you.

Key terms

File object

The object returned by open(), through which a program reads or writes.

Context manager

The with statement, which closes the file when its block ends.

3. Reading and writing text files

Writing: write(s) writes one string and returns its length; writelines(list) writes each string. Neither adds \n. Convert numbers with str().

Reading: read() → whole remaining text as one string; read(n) → n characters; readline() → one line ("" at the end of the file); readlines() → list of lines; for line in f: → one line per pass.

Output of the writing example (run in Python):

Line one
Line two
Line three
read() returns one string; readline() one line; readlines() a list of lines (shown cut at 70 characters).
Call on a freshly opened hills.txtReturns (run in Python)
f.read()'Clouds gather over the hills\nthe bus waits at the stop\ntea stalls o
f.read(6)'Clouds'
f.readline()'Clouds gather over the hills\n'
f.readlines()[1]'the bus waits at the stop\n'
Neither method adds a newline: put \n in the strings yourself.
StatementEffect
n = f.write("Line one\n")writes one string and returns its length: n is 9
f.writelines(["Line two\n", "Line three\n"])writes each string of the list, adding nothing between them
File afterwardsLine one, Line two, Line three

4. seek(), tell() and processing lines

tell() gives the file pointer's position in bytes; seek(n) moves it to byte n (seek(0) = start).

Typical processing tasks: count lines, words (split()), characters (len()), or lines that start with a letter; display long words; copy selected lines to another file opened in "w" mode.

with open("routes.txt") as src, open("caps.txt", "w") as dst:
    for line in src:
        if line[0].isupper():
            dst.write(line)

Resulting caps.txt (run in Python):

Route 7 Kothrud
Route 3 Hadapsar
Route 21 Wakad
The file pointer marks where the next read or write happens.
Positions are counted in bytes from the start of the file.
Step on stock.txttell() beforeReturned
readline()0'pens 40\n'
read(7)8'erasers'
seek(0), then read(4)0'pens'
Dry run produced by running the loop in Python.
LineWords in lineCharacters (with \n)Words so farCharacters so far
1529529
26261155
36271782
442321105

5. Binary files with pickle

import pickle. Pickling (pickle.dump(obj, f)) writes an object as bytes; unpickling (pickle.load(f)) reads the next object back.

  • Open in "wb" (new file), "ab" (add records) or "rb" (read).
  • Read till the end: call load() in while True inside try, and stop on except EOFError.
  • Search: load each record and test it; use a flag to report "not found".
  • Update: load all records into a list, change in memory, dump all again in "wb".

Appending a fifth record and counting again (run in Python):

import pickle
with open("books.dat", "ab") as f:
    pickle.dump([105, "Blue Kettle", 210], f)
n = 0
with open("books.dat", "rb") as f:
    try:
        while True:
            pickle.load(f)
            n += 1
    except EOFError:
        pass
print(n)

Output: 5

Each dump() writes one object; each load() reads the next one.
The EOFError is expected: it means every record has been read.
Pickled records can differ in length, so the whole file is rewritten.

Key terms

Pickling

Converting a Python object into a byte stream (serialisation) with dump().

Unpickling

Rebuilding the object from the byte stream (deserialisation) with load().

EOFError

Raised by load() when no object is left in the file.

6. CSV files

import csv. Open with newline="" so that the writer controls line endings.

  • Writing: w = csv.writer(f), then w.writerow(list) or w.writerows(list_of_lists).
  • Reading: for row in csv.reader(f): gives each line as a list of strings; next(reader) skips the header; convert numbers with int().
import csv
with open("marks.csv", newline="") as f:
    for row in csv.reader(f):
        print(row)

Output (run in Python):

['roll', 'name', 'marks']
['1', 'Aarav', '78']
['2', 'Bina', '91']
['3', 'Chetan', '64']
['4', 'Divya', '85']
Table of the steps for writing a CSV file: import csv, open with newline equal to empty string, create a writer, write one row with writerow, write many with writerows, and close
StepCode
Import the moduleimport csv
Open with newline=""f = open("marks.csv", "w", newline="")
Create a writerw = csv.writer(f)
Write one roww.writerow(["roll", "name", "marks"])
Write many rowsw.writerows([[1, "Aarav", 78], [2, "Bina", 91]])
Closef.close() (or use with)
Comparison table of text, binary and CSV files: what each stores, whether it is human-readable, how it is written and read, and the types of the values read back
FeatureTextBinary (pickle)CSV
Storescharacters in linesbytes of Python objectscomma-separated text
Human-readable?YesNoYes
Write withwrite(), writelines()pickle.dump()writerow(), writerows()
Read withread(), readline(), readlines()pickle.load() until EOFErrorcsv.reader()
Types after readingstrthe original typesstr in every field

Key terms

CSV

Comma-separated values: a text file holding a table, one record per line.

Delimiter

The character that separates fields (a comma by default).

Exam tips

Common mistakes

  • Opening an existing file in "w" when the task says add: the old data are lost.
  • Forgetting \n in write(), or comparing a line without strip().
  • Writing numbers without str() (TypeError).
  • Using text modes with pickle, or forgetting import pickle.
  • Reading a binary file with a fixed loop count instead of handling EOFError.
  • Adding CSV fields without int(), or forgetting to skip the header.

How marks are usually awarded

  • Output questions: each correct line of output.
  • Functions on files: importing the module, opening in the right mode, the loop, the condition or logic, and the result.
  • Error-finding: 1 mark per error identified and corrected.