Skip main navigation

Text files and binary files

Learn how to read a binary file in Python using a buffer. This is how computer systems read data such as JPEG images.
Robot reading a 'Human 2 Binary Phrase Book'
Python can create files for humans to read and it can also create files intended for only the computer to read.

In the previous steps you have been writing and reading data to text files. For each project text files were created and saved in a manner that a human can read if they wished, but in reality the text files have already been interpreted by the computer. Computers do not understand human language and so values are written in a way that a computer can process, known as binary.

Binary files can be used to store any data; for example, a JPEG image is a binary file designed to be read by a computer system. The data inside a binary file is stored as raw bytes, which is not human readable.

Using a binary file

Here is a short program. Read the code line by line; can you work out what the code will do?

data = [100,24,255]
buffer = bytes(data)
print(buffer)
f = open("binary.txt", "bw")
f.write(buffer)
f.close()

How does this code work?

The program starts by creating a list called data, containing three numbers. The data list is then converted to bytes (binary numbers) and stored in the buffer variable, which is then printed to the screen. The list has been converted from something you can read into something that is more efficient for a computer system to handle.

A new file called binary.txt is created and the mode set to bw so that bytes can be written into the file, making it a binary file.

The contents of the buffer variable are then written to this file, which is then closed.

Test the code

Copy the code above into your Python editor, save it, and run the code. You can look at the contents of binary.txt using a text editor.

the above program running in Mu with the output b'dx18xff'

Does it match what you saw in the REPL/console?

Your text editor has tried to open the file, but despite trying it has failed to read the contents correctly. This is because the text editor is trying to interpret the file contents as text.

Reading the file

Reading binary files is very similar to reading text files. Have a look at this code and see if you can work out what it does.

f = open("binary.txt", "br")
binary = f.read()
print(binary)
data = list(binary)
print(data)
f.close()

This code opens the file and sets the mode to binary read, "br". It then reads the contents of the file into the variable binary and prints it to the screen.

The contents of the binary variable are still formatted as bytes. This is converted back into a list using the list() function. This list is then stored in the variable data, having been restored to its original format.

When you run this code, you should see the following:

  • The list is printed once after it has been converted into binary, and again after the file has been read.
  • The original list converted back from the binary data.

In this step you learnt more about how Python can work with files, for both humans and machines to understand.

Can you think of another type of binary file? Add your suggestions to the comments section.

This article is from the free online

Programming 103: Saving and Structuring Data

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education