This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Introduction

You will likely be working with reading and writing from files a lot throughout your degree.

In this module, we will take a look at how to do this in Python.

What are files?

A file is a named location on disk and is used to store data on the hard disk.

The hard disk remembers your file even after you switch it off (non-volalite memory). Contrast this to a RAM which loses its data when you turn your computer off (volatile).

To work on a file, you will need to:

  1. Open the file
  2. Perform some operations on the file (read or write)
  3. Close the file

Python handles two types of files: binary and text files.

Text files are plain text files that are human readable, and they are made up of sequences of characters. You can view these files in a text editor. Your Python scripts are examples of text files. There are often \n characters at the end of each line in the file to mark a new line (these may actually be \r\n if the files were created on a Windows machine).

Most files on your computer are binary files. For example image files like .jpg, .png, .bmp and documents like .pdf, .docx, .xlsx. The content of these files are not directly human-readable in a text editor, and can only be viewed when opened in an application that ‘understands’ how to interpret the file.