Text files and binary files
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.
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.