Do you want to read the binary file using Python? This article will show you how you can read a binary file in Python with examples.
It is simple to read a binary file in Python while opening the file using the with open() the method you need to use mode as ‘rb’ if you only want to read the file and if you want to write as well then you need to set the mode as ‘wb’.
Syntax to Open File in Binary Read or Write Mode in Python
Below is the syntax to open any binary file in read-only mode or read-write mode. If you have a file that you want only to read the lines then you must open the file in read-only mode else if you want to write inside the same file after read use the read-write mode.
#For Read Only Mode with open('filename with Path', mode='rb') #For Read-Write Mode with open('Filename with path', mode='wb')
Now let us see the various examples of how you can read or write the binary files in Python in different ways.
Read Binary File In Python Example
If you have a binary file and you want to read the file line by line then you can use the below code lines to do it.
You can modify these codes as per your file path and location. I will simplify the code for better understanding for further modification.
In the below example we are assuming that you already have a binary file saved on your disk and you are trying to read it using python.
#Opening a binary File in Read Only Mode with open('C:\Test\\slick.bin', mode='rb') as binaryFile: lines = binaryFile.readlines() print(lines)
In the above code, we are opening the slick.bin file using the open method in read-only mode as binaryFile. Then we are reading all the lines in the file in the lines variable above and print all the lines read in form of a list.
So as an output you will get the list of lines read, in common terms, an array of all the lines in a binary file will be saved to lines variable.
Write Binary File In Python Example
Writing a binary file is similarly simple as shown in the above code for reading the binary file. All you need to do is change the mode as ‘wb’ and then write the lines you want to write and after that, you can read those lines as well.
Let us see in the below example code on the usage of writing binary files in Python.
#Opening a binary File in Read Only Mode with open('C:\Test\\TestBinaryFile.bin', mode='wb') as binaryFile: #Assiging a Binary String lineToWrite = b'You are on Coduber.' #Writing the binary String to File. binaryFile.write(lineToWrite) #Closing the File after Writing binaryFile.close() #reading the lines from Binary File with open('C:\Test\\TestBinaryFile.bin', mode='rb') as binaryFile: readTheLinesWritten = binaryFile.read() #Printing the Lines Written and read from the Binary File print(readTheLinesWritten.decode('utf-8'))
You are on Coduber.
Note that after writing to the binary file you need to close the binary file in order to get it saved and to read any binary file you need to re-open the binary file using ‘rb’ mode.
I hope you were able to learn how to read a binary file in Python and how to write a binary file in python. You can easily achieve those things using the above code mentioned.
Let me know if I was able to solve your problem or you have any better solution. I will be happy to provide a resolution for it.