- Binary Files
- How to Read a Binary File
- Open a binary file in a read mode
- Open a binary file in a read mode using the with statement
- How to Write a Binary File
- Open a file in a binary mode
- A List of the File Modes for Binary Files
- Python File I/O — Read and Write Files
- Reading File
- Reading a Line
- Reading All Lines
- Reading Binary File
- Writing to a File
- Create a new File and Write
- Appending to an Existing File
- Write Multiple Lines
- Writing to a Binary File
Binary Files
Binary files are computer files that contain data in a format that can be easily read and manipulated by machines. Binary files are important because they allow programmers to work with data in a way that is both efficient and secure. This article will discuss the basics of binary files in Python, how to read and write them, and some common use cases where they are most beneficial.
How to Read a Binary File
In Python, we can use the open() function to open a binary file and read the contents.
Open a binary file in a read mode
file = open("example.bin", "rb") # Read the contents of the file and store it in a variable binary_data = file.read() # Close the file file.close() # Print the contents of the file print(binary_data)
- We open the binary file example.bin using the open() function, with the mode rb (read binary).
- We read the contents of the file using the .read() method and store it in the variable binary_data .
- We close the file using the .close() method.
- We print the contents of the file using the print() function.
Open a binary file in a read mode using the with statement
with open("example.bin", "rb") as file: binary_data = file.read() # Print the contents of the file print(binary_data)
- We open the binary file example.bin using the open() function and the mode rb (read binary) using with statement.
- We read the contents of the file using the .read() method and store it in the variable binary_data .
- We print the contents of the file using the print() function.
Using the with statement to open a file ensures that the file is closed automatically after reading its contents.
How to Write a Binary File
To write a binary file, you need to use the built-in open() function with a mode parameter of wb . This will open the file in binary mode, allowing you to write binary data to it. Here are the steps to write a binary file:
- Open the file in binary mode using the open() function with a mode parameter of wb .
- Write the binary data to the file using the write() method of the file object.
- Close the file using the close() method of the file object.
Open a file in a binary mode
file = open('binaryfile.bin', 'wb') try: ##### Write binary data to file file.write(b'\x00\x01\x02\x03\x04\x05') finally: ### Close the file file.close()
In conclusion, writing binary files is a simple process that involves opening the file in binary mode and writing binary data to it using the write() method of the file object.
A List of the File Modes for Binary Files
When working with binary files, you need to open them in the correct file mode to ensure the file is being read and/or written correctly. There are six file modes for binary files:
- rb: Read mode (binary) — opens the file for reading in binary format.
- rb+: Read and write mode (binary) — opens the file for reading and writing in binary format.
- wb: Write mode (binary) — opens the file for writing in binary format. If the file already exists, it will be truncated.
- wb+: Write and read mode (binary) — opens the file for reading and writing in binary format. If the file already exists, it will be truncated.
- ab: Append mode (binary) — opens the file for writing in binary format. New data will be written at the end of the file.
- ab+: Append and read mode (binary) — opens the file for reading and writing in binary format. New data will be written at the end of the file.
with open("file.bin", "rb") as f: data = f.read() print(data)
This code opens a binary file named file.bin in read mode using the with statement. The rb mode ensures the file is read in binary format. The read() method is used to read the entire file and the content is then printed out to the console.
with open("file.bin", "wb") as f: data = b"\x48\x65\x6c\x6c\x6f" # Hello in binary f.write(data)
This code creates a binary file named file.bin in write mode using the with statement. The wb mode ensures the file is written in binary format. The b prefix before the string literal indicates that the string is in binary format. The write() method is used to write binary data to the file. This code writes the binary data for the string Hello to the file.
Python File I/O — Read and Write Files
In Python, the IO module provides methods of three types of IO operations; raw binary files, buffered binary files, and text files. The canonical way to create a file object is by using the open() function.
Any file operations can be performed in the following three steps:
- Open the file to get the file object using the built-in open() function. There are different access modes, which you can specify while opening a file using the open() function.
- Perform read, write, append operations using the file object retrieved from the open() function.
- Close and dispose the file object.
Reading File
File object includes the following methods to read data from the file.
- read(chars): reads the specified number of characters starting from the current position.
- readline(): reads the characters starting from the current reading position up to a newline character.
- readlines(): reads all lines until the end of file and returns a list object.
The following C:\myfile.txt file will be used in all the examples of reading and writing files.
This is the first line. This is the second line. This is the third line.
The following example performs the read operation using the read(chars) method.
f = open('C:\myfile.txt') # opening a file lines = f.read() # reading a file print(lines) #'This is the first line. \nThis is the second line.\nThis is the third line.' f.close() # closing file object
Above, f = open(‘C:\myfile.txt’) opens the myfile.txt in the default read mode from the current directory and returns a file object. f.read() function reads all the content until EOF as a string. If you specify the char size argument in the read(chars) method, then it will read that many chars only. f.close() will flush and close the stream.
Reading a Line
The following example demonstrates reading a line from the file.
f = open('C:\myfile.txt') # opening a file line1 = f.readline() # reading a line print(line1) #'This is the first line. \n' line2 = f.readline() # reading a line print(line2) #'This is the second line.\n' line3 = f.readline() # reading a line print(line3) #'This is the third line.' line4 = f.readline() # reading a line print(line4) #'' f.close() # closing file object
As you can see, we have to open the file in ‘r’ mode. The readline() method will return the first line, and then will point to the second line in the file.
Reading All Lines
The following reads all lines using the readlines() function.
f = open('C:\myfile.txt') # opening a file lines = f.readlines() # reading all lines print(lines) #'This is the first line. \nThis is the second line.\nThis is the third line.' f.close() # closing file object
The file object has an inbuilt iterator. The following program reads the given file line by line until StopIteration is raised, i.e., the EOF is reached.
f=open('C:\myfile.txt') while True: try: line=next(f) print(line) except StopIteration: break f.close()
Use the for loop to read a file easily.
f=open('C:\myfile.txt') for line in f: print(line) f.close()
This is the first line. This is the second line. This is the third line.
Reading Binary File
Use the ‘rb’ mode in the open() function to read a binary files, as shown below.
f = open('C:\myimg.png', 'rb') # opening a binary file content = f.read() # reading all lines print(content) #print content f.close() # closing file object
Writing to a File
The file object provides the following methods to write to a file.
- write(s): Write the string s to the stream and return the number of characters written.
- writelines(lines): Write a list of lines to the stream. Each line must have a separator at the end of it.
Create a new File and Write
The following creates a new file if it does not exist or overwrites to an existing file.
f = open('C:\myfile.txt','w') f.write("Hello") # writing to file f.close() # reading file f = open('C:\myfile.txt','r') f.read() #'Hello' f.close()
In the above example, the f=open(«myfile.txt»,»w») statement opens myfile.txt in write mode, the open() method returns the file object and assigns it to a variable f . ‘w’ specifies that the file should be writable. Next, f.write(«Hello») overwrites an existing content of the myfile.txt file. It returns the number of characters written to a file, which is 5 in the above example. In the end, f.close() closes the file object.
Appending to an Existing File
The following appends the content at the end of the existing file by passing ‘a’ or ‘a+’ mode in the open() method.
f = open('C:\myfile.txt','a') f.write(" World!") f.close() # reading file f = open('C:\myfile.txt','r') f.read() #'Hello World!' f.close()
Write Multiple Lines
Python provides the writelines() method to save the contents of a list object in a file. Since the newline character is not automatically written to the file, it must be provided as a part of the string.
lines=["Hello world.\n", "Welcome to TutorialsTeacher.\n"] f=open("D:\myfile.txt", "w") f.writelines(lines) f.close()
Opening a file with «w» mode or «a» mode can only be written into and cannot be read from. Similarly «r» mode allows reading only and not writing. In order to perform simultaneous read/append operations, use «a+» mode.
Writing to a Binary File
The open() function opens a file in text format by default. To open a file in binary format, add ‘b’ to the mode parameter. Hence the «rb» mode opens the file in binary format for reading, while the «wb» mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable. When opened using any text editor, the data is unrecognizable.
The following code stores a list of numbers in a binary file. The list is first converted in a byte array before writing. The built-in function bytearray() returns a byte representation of the object.
f=open("binfile.bin","wb") num=[5, 10, 15, 20, 25] arr=bytearray(num) f.write(arr) f.close()