Source

Adapted from my own handwritten and hand-typed notes from Year 1 to Year 3 of my EEE degree

Opening and Closing

open(filename, mode) returns a Python file object. Use the full path if the file is not in the current working directory, and call close() when done.

ModeMeaning
'r'read (default)
'w'write, creates the file or overwrites it if it exists
'x'write, creates the file, fails if it already exists
'a'write, creates the file or appends to it if it exists
't'text (default)
'b'binary
'+'read and write, e.g. 'r+', 'w+', 'a+'

The default mode is 'rt'.

Reading a Text File

f = open('sonnet18.txt')
whole = f.read()        # entire file as one str
f.seek(0)               # move the file position back to the start
line = f.readline()     # one line, up to and including '\n'
f.seek(0)
lines = f.readlines()   # list of str, one item per line
f.close()

Things to remember:

  • white space at the end of lines is kept
  • the newline '\n' is a separate character inside the string
  • readline can be repeated until EOF (end of file), where it returns ''

To get words out of a line use split, and to get numbers use type casting, both covered in String Operations in Python.

Writing a Text File

verse1 = 'All that is gold does not glitter,\n'
f = open('tolkien.txt', 'w')      # overwrite; use 'a' to append
f.write(verse1)                   # '\n' sends the next text to a new line
f.write('Not all those who wander are lost.')
f.close()

Each write continues from the current position of the file object.

Example: Append Rainfall Records

days = [24, 25, 26, 27, 28, 29, 30]
rain = [0.0, 3.2, 15.6, 0.2, 0.0, 0.0, 43.8]
f = open('Changi201806.txt', 'a')
for d, r in zip(days, rain):
    f.write(str(d) + ',' + str(r) + '\n')
f.close()

Other File Formats

Text files are inefficient for large data. Better options:

  • pickle: serialises Python objects directly in binary, but only for use from Python
  • HDF5 (via h5py): very large numerical data, language independent, can slice into multi-TB files without loading all of it
  • JSON: text based, human readable, language independent format for standard Python objects