How To Read Binary File In Python - Detailed Guide - Stack Vidhya (2024)

Binary files are files that are not normal text files. Example: An Image File. These files are also stored as a sequence of bytes in the computer hard disk. These types of binary files cannot be opened in the normal mode and read as text.

You can read binary file by opening the file in binary mode using the open('filename', 'rb').

When working with the problems like image classification in Machine learning, you may need to open the file in binary mode and read the bytes to create ML models. In this situation, you can open the file in binary mode, and read the file as bytes. In this case, decoding of bytes to the relevant characters will not be attempted. On the other hand, when you open a normal file in the normal read mode, the bytes will be decoded to string or the other relevant characters based on the file encoding.

If you’re in Hurry

You can open the file using open() method by passing b parameter to open it in binary mode and read the file bytes.

open('filename', "rb") opens the binary file in read mode.

r– To specify to open the file in reading mode
b – To specify it’s a binary file. No decoding of bytes to string attempt will be made.

Example

The below example reads the file one byte at a time and prints the byte.

try: with open("c:\temp\Binary_File.jpg", "rb") as f: byte = f.read(1) while byte: # Do stuff with byte. byte = f.read(1) print(byte)except IOError: print('Error While Opening the file!') 

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn how to read binary files in different ways.

Table of Contents

Read binary file byte by byte

In this section, you’ll learn how to read a binary file byte by byte and print it. This is one of the fastest ways to read the binary file.

The file is opened using the open() method and the mode is mentioned as “rb” which means opening the file in reading mode and denoting it’s a binary file. In this case, decoding of the bytes to string will not be made. It’ll just be read as bytes.

The below example shows how the file is read byte by byte using the file.read(1) method.

The parameter value 1 ensures one byte is read during each read() method call.

Example

try: with open("c:\temp\Binary_File.jpg", "rb") as f: byte = f.read(1) while byte: # Do stuff with byte. byte = f.read(1) print(byte)except IOError: print('Error While Opening the file!') 

Output

 b'\xd8' b'\xff' b'\xe0' b'\x00' b'\x10' b'J' b'F' b'I' b'F' b'\x00' b'\x01' b'\x01' b'\x00' b'\x00' b'\x01' b'\x00' b'\x01' b'\x00' b'\x00' b'\xff' b'\xed' b'\x00' b'|' b'P' b'h' b'o' b't' b'o' b's' b'h' b'o' b'p' b' ' b'3' b'.' b'0' b'\xc6' b'\xb3' b'\xff' b'\xd9' b''

Python Read Binary File into Byte Array

In this section, you’ll learn how to read the binary files into a byte array.

First, the file is opened in therb mode.

A byte array called mybytearray is initialized using the bytearray() method.

Then the file is read one byte at a time using f.read(1) and appended to the byte array using += operator. Each byte is appended to the bytearray.

At last, you can print the bytearray to display the bytes that are read.

Example

try: with open("c:\temp\Binary_File.jpg", "rb") as f: mybytearray = bytearray() # Do stuff with byte. mybytearray+=f.read(1) mybytearray+=f.read(1) mybytearray+=f.read(1) mybytearray+=f.read(1) mybytearray+=f.read(1) print(mybytearray)except IOError: print('Error While Opening the file!') 

Output

 bytearray(b'\xff\xd8\xff\xe0\x00\x10')

Python read binary file into numpy array

In this section, you’ll learn how to read the binary file into a NumPy array.

First, import numpy as np to import the numpy library.

Then specify the datatype as bytes for the np object using np.dtype('B')

Next, open the binary file in reading mode.

Now, create the NumPy array using the fromfile() method using the np object.

Parameters are the file object and the datatype initialized as bytes. This will create a NumPy array of bytes.

numpy_data = np.fromfile(f,dtype)

Example

import numpy as npdtype = np.dtype('B')try: with open("c:\temp\Binary_File.jpg", "rb") as f: numpy_data = np.fromfile(f,dtype) print(numpy_data)except IOError: print('Error While Opening the file!') 

Output

[255 216 255 ... 179 255 217]


The bytes are read into the numpy array and the bytes are printed.

Read binary file Line by Line

In this section, you’ll learn how to read binary file line by line.

You can read the file line by line using the readlines() method available in the file object.

Each line will be stored as an item in the list. This list can be iterated to access each line of the file.

rstrip() method is used to remove the spaces in the beginning and end of the lines while printing the lines.

Example

f = open("c:\temp\Binary_File.jpg",'rb')lines = f.readlines()for line in lines: print(line.rstrip())

Output

 b'\x07\x07\x07\x07' b'' b'' b'' b'' b'' b'\x0c\x0f\x0c\x0c\x0c\x0c\x0c\x0c\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x12\x12\x12\x12\x12\x12\x15\x15\x15\x15\x15\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\xff\xdb\x00C\x01\x04\x04\x04\x06\x06\x06' b'\x06\x06'

Read Binary File Fully in One Shot

In this section, you’ll learn how to read binary file in one shot.

You can do this by passing -1 to the file.read() method. This will read the binary file fully in one shot as shown below.

Example

try: f = open("c:\temp\Binary_File.jpg", 'rb') while True: binarycontent = f.read(-1) if not binarycontent: break print(binarycontent)except IOError: print('Error While Opening the file!')

Output

 b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xed\x00|Photoshop 3.0\x008BIM\x04\x04\x00\x00\x00\x00\x00\x1c\x02(\x00ZFBMD2300096c010000fe0e000032160000051b00003d2b000055300000d6360000bb3c0000ce4100008b490000\x00\xff\xdb\x00C\x00\x03\x03\x03\x03\x03\x03\x05\x03\x03\x05\x07\x05\x05\x05\x07\n\x07\x07\x07\x07\n\x0c\n\n\n\n\n\x0c\x0f\x0c\x0c\x0c\x0c\x0c\x0c\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x12\x12\x12\x12\x12\x12\x15\x15\x15\x15\x15\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\xff\xdb\x00C\x01\x04\x04\x04\x06\x06\x06\n\x06\x06\n\x18\x11\x0e\x11\x18\x18\x18\x18\x18\x18\x18\x18\x18\x18\x18\x18\x18\x18\x18

Python Read Binary File and Convert to Ascii

In this section, you’ll learn how to read a binary file and convert to ASCII using the binascii library. This will convert all the bytes into ASCII characters.

Read the file as binary as explained in the previous section.

Next, use the method binascii.b2a_uu(bytes). This will convert the bytes into ascii and return an ascii value.

Then you can print this to check the ascii characters.

Example

import binasciitry: with open("c:\temp\Binary_File.jpg", "rb") as f: mybytes = f.read(45) data_bytes2ascii = binascii.b2a_uu(mybytes) print("Binary String to Ascii") print(data_bytes2ascii)except IOError: print("Error While opening the file!")

Output

 Binary String to Ascii b'M_]C_X 02D9)[emailprotected] ! 0 0 ! #_[0!\\4&AO=&]S:&]P(#,N, X0DE-! 0 \n'

Read binary file into dataframe

In this section, you’ll learn how to read the binary file into pandas dataframe.

First, you need to read the binary file into a numpy array. Because there is no method available to read the binary file to dataframe directly.

Once you have the numpy array, then you can create a dataframe with the numpy array.

Pass the NumPy array data into the pd.DataFrame(). Then you’ll have the dataframe with the bytes read from the binary file.

Example

import numpy as npimport pandas as pd# Create a dtype with the binary data format and the desired column namestry: dt = np.dtype('B') data = np.fromfile("c:\temp\Binary_File.jpg", dtype=dt) df = pd.DataFrame(data) print(df)except IOError: print("Error while opening the file!")

Output

 0 0 255 1 216 2 255 3 224 4 0 ... ... 18822 0 18823 198 18824 179 18825 255 18826 217 [18827 rows x 1 columns]

This is how you can read a binary file using NumPy and use that NumPy array to create the pandas dataframe.

With the NumPy array, you can also read the bytes into the dictionary.

Read binary file skip header

In this section, you’ll learn how to read binary file, skipping the header line in the binary file. Some binary files will be having the ASCII header in them.

This skip header method can be useful when reading the binary files with the ASCII headers.

You can use the readlines() method available in the File object and specify [1:] as an additional parameter. This means the line from index 1 will be read.

The ASCII header line 0 will be ignored.

Example

f = open("c:\temp\Binary_File.jpg",'rb')lines = f.readlines()[1:]for line in lines: print(line.rstrip())

Output

 b'\x07\x07\x07\x07' b'' b'' b'' b'' b'' b'\x0c\x0f\x0c\x0c\x0c\x0c\x0c\x0c\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x12\x12\x12\x12\x12\x12\x15\x15\x15\x15\x15\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\xff\xdb\x00C\x01\x04\x04\x04\x06\x06\x06' b'\x06\x06' b"\x93\x80\x18\x98\xc9\xdc\x8bm\x90&'\xc5U\xb18\x81\xc7y\xf0\x80\x00\x14\x1c\xceQd\x83\x13\xa0\xbf-D9\xe0\xae;\x8f\\LK\xb8\xc3\x8ae\xd4\xd1C\x10\x7f\x02\x02\xa6\x822K&D\x9a\x04\xd4\xc8\xfbC\x87\xf2\x8d\xdcN\xdes)rq\xbbI\x92\xb6\xeeu8\x1d\xfdG\xabv\xe8q\xa5\xb6\xb56\xe0\xa1\x06\x84n#\xf0\x1c\x86\xb0\x83\xee\x99\xe7\xc6\xaaN\xafY\xdf\xd9\xcfe\xd5\x84" b'\xd9\x0b\xc2\x1b0\xa1Q\x17\x88\xb4et\x81u8\xed\xf5\xe8\xd9#c\t\xf9\xc0\xa7\x06\xa2/={\x87l\x01K\x870\xe3\xa1\x024\xdc^\x11\x96\x96\xba\[emailprotected]\x91A\xd6U\xea\xe1\xbb\xb733'

Readind Binary file using Pickle

In this section, you’ll learn how to read binary files in python using the Pickle.

This is really tricky as all the types of binary files cannot be read in this mode. You may face problems while pickling a binary file. As invalid load key errors may occur.

Hence it’s not recommended to use this method.

Example

import picklefile_to_read = open("c:\temp\Binary_File.jpg", "rb")loaded_dictionary = pickle.load(file_to_read)print(loaded_dictionary)

Output

 --------------------------------------------------------------------------- UnpicklingError Traceback (most recent call last) <ipython-input-23-dea0d83e3f49> in <module> 7 file_to_read = open("E:\Vikram_Blogging\Stack_Vidhya\Python_Notebooks\Read_Binary_File_Python\Binary_File.jpg", "rb") 8 ----> 9 loaded_dictionary = pickle.load(file_to_read) 10 11 print(loaded_dictionary) UnpicklingError: invalid load key, '\xff'.

Conclusion

Reading a binary file is an important functionality. For example, reading the bytes of an image file is very useful when you are working with image classification problems. In this case, you can read the image file as binary and read the bytes to create the model.

In this tutorial, you’ve learned the different methods available to read binary files in python and the different libraries available in it.

If you have any questions, feel free to comment below.

How To Read Binary File In Python - Detailed Guide - Stack Vidhya (2024)

FAQs

How to read data from binary file in Python? ›

Reading binary files in Python is easy with the `open()` function. You can open a file for reading by setting the mode parameter to `'rb'`, and then use either the `read()` or looping methods to read data from it.

How do I read the contents of a binary file? ›

To read from a binary file

Use the ReadAllBytes method, which returns the contents of a file as a byte array. This example reads from the file C:/Documents and Settings/selfportrait.

How to read binary image in Python? ›

Steps
  1. Import the required library. In all the following examples, the required Python library is OpenCV. ...
  2. Read an the input image using cv2. imread(). ...
  3. Now convert this BGR image to grayscale image as below using cv2. cvtColor() function. ...
  4. Apply thresholding cv2. ...
  5. Display the converted binary image.
Dec 2, 2022

How to read binary file rb in Python? ›

Files are automatically opened in text mode in Python. When choosing a mode, include the letter "b" for binary mode. By default, the open() function opens a file in text format. As a result, the "wb" mode opens the file in binary format for writing while the "rb" option opens the file in binary format for reading.

How can I decode a binary file? ›

You can use Notepad++ install the plugin for hex editor. Once you have that, all you need to do is some kind of combination and permutation, depending upon what kind of data is held in you binary file (also while doing it keep in mind the byte order little or big endian).

How to decode binary code python? ›

the common ways to convert binary strings to and from ASCII text in Python are:
  1. Use the binascii module to convert Binary String to ASCII.
  2. Binary String to ASCII using Int. to_bytes() function.
  3. ASCII to Binary String using Int. from_bytes() function.
Jul 16, 2022

How do I convert a binary file to readable? ›

How to Use Binary Code Translator?
  1. Step 1: Paste the binary code into the box you want to convert to plain text. ...
  2. Step 2: Click the “Convert” button for conversion.
  3. Step 3: The converted plain text will appear in the right side box immediately.
  4. Step 4: Copy the output text or download the .txt file to your device.

Which command is used to see the content of binary file? ›

Using the od Command

Similarly, we can use the od command to get the string in a binary file. Typically, od displays the contents of a file in octal, decimal, hexadecimal, or ASCII format.

What tool reads binary files? ›

Applications To Open A BIN File
  • #1) NTI Dragon Burn 4.5.
  • #2) Roxio Creator NXT Pro 7.
  • #3) DT Soft DAEMON Tools.
  • #4) Smart Projects IsoBuster.
  • #5) PowerISO.
Mar 20, 2023

How to read a file in Python? ›

To read a text file in Python, you follow these steps:
  1. First, open a text file for reading by using the open() function.
  2. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object.
  3. Third, close the file using the file close() method.

How do I edit a binary file in Python? ›

  1. You dont need to read the whole file into memory for changing the first 4 bytes.
  2. Open the file in mode 'r+b'
  3. Use f.seek(0) to seek to the begining.
  4. Write 4 bytes of data using f.write('ABCD')
  5. Close the file.
Jan 5, 2011

What is binary format in Python? ›

PythonServer Side ProgrammingProgramming. "Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default.

How to read binary data in pandas? ›

  1. you need to put it into a numpy array (or python dict/list). is it a custom format? or something like stata? ...
  2. It's a custom format. ...
  3. your best bet is prob just read with python and create a numpy array; if speed is a problem, then u can read with cython, or if u already have a reader in c then u can wrap in cython.
May 15, 2013

How to check if binary file exists in Python? ›

One way is using isfile() function of os. path module. The function returns true if file at specified path exists, otherwise it returns false.

What does := do in Python? ›

Assignment expressions

There is new syntax := that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.

How do you decode a value in python? ›

Python String encode() Method

The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

Does python understand binary? ›

Python provides the binary number system to the user, in which we convert binary numbers to decimal numbers, vice versa, and binary to octal number system, which means as per our requirement, we can convert the binary numbers into the other number system.

How to decode input in python? ›

We use the encode() method on the input string, which every string object has. This encodes input_string using encoding , where errors decides the behavior to be followed if, by any chance, the encoding fails on the string. encode() will result in a sequence of bytes .

Can we read a binary file? ›

Binary files are not human readable and require a special program or hardware processor that knows how to read the data inside the file. Only then can the instructions encoded in the binary content be understood and properly processed.

Why are binary files not readable? ›

A binary file is a file that stores information in the form of bits and bytes (0's and 1's). Binary files are not human-readable because the bytes they contain translate to characters and symbols that have many other non-printable characters.

How do you convert binary code? ›

To convert binary integer to decimal, start from the left. Take your current total, multiply it by two and add the current digit. Continue until there are no more digits left. Here is an example of such conversion using the fraction 1011.

Which class would I use to read a binary file? ›

The BinaryReader class is used to read binary data from a file. A BinaryReader object is created by passing a FileStream object to its constructor.

Which methods are used to write and read into from a binary file? ›

Read, write and seek operations can be performed on binary files with the help of fread(), fwrite() and fseek() functions, respectively. After reading or writing a structure, the file pointer is moved to the next structure. The fseek() function can move the pointer to the position as requested.

Which method is used to take data from a binary file? ›

Using load() method. (i). load(): In python programming, load() method is used to read data from a binary file. It takes file object as an argument.

How do I read a file in Python by line? ›

If you want to read only one single individual line from a text file, use the readline() method: with open("example. txt") as file: print(file. readline()) # output # I absolutely love coding!

How do I change data in a binary file? ›

Update record in Binary file
  1. Open the file using read mode.
  2. Declare a variable for unique value to be updated.
  3. Use try-except and while loop as explained above.
  4. Add record fetched from binary file into a list.
  5. Enter the new record information to update.
Aug 6, 2020

How do you write text in a binary file in Python? ›

To write a binary string to a binary file, you need to open the file in “binary write” mode using 'wb' as the second positional argument of the open() function. For instance, you'd write open('my_file. bin', 'wb') instead of open('my_file. bin', 'w') .

Can Python read and write binary files? ›

Reading and Writing Binary data

Reading and writing binary file is done by appending b to the mode string. In Python 3, the binary data is represented using a special type called bytes . The bytes type represents an immutable sequence of numbers between 0 and 255.

How to convert binary file to CSV in Python? ›

To read a binary file, use the open('rb') function within a context manager ( with keyword) and read its content into a string variable using f. readlines() . You can then convert the string to a CSV using various approaches such as the csv module.

Is Python file binary or text? ›

In Python, files can be opened in two modes: text mode and binary mode. Text mode is the default mode, and it is used for reading and writing text files, while the binary mode is used for reading and writing binary files. Text files are files that contain text data, such as strings or characters.

How do you read and understand binary numbers? ›

To understand a number in binary, for whole numbers we need to recognise that the most significant binary digit (or bit for short) is on the left and least significant bit is on the right. As we look right to left, each bit represents a higher power of 2 (because binary is base 2).

How can we detect end of file in a binary file in Python? ›

An empty string is returned when EOF is encountered immediately.
  1. f. read(1) will return a byte object containing either 1 byte or 0 byte is EOF was reached.
  2. f. read(2) will return a byte object containing either 2 bytes, or 1 byte if EOF is reached after the first byte, or 0 byte if EOF in encountered immediately.
  3. ...
Aug 23, 2014

How to check type of file in Python? ›

We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values - root and extension.

How do you check if a binary file is empty Python? ›

Check if the File is Empty using os.

getsize() to check whether a file is empty or not. It takes the file path as an argument. It will check for the file size. If the file size is 0, it will print the file as Empty.

What are the 7 operators in Python? ›

Python Operators
  • Arithmetic operators.
  • Assignment operators.
  • Comparison operators.
  • Logical operators.
  • Identity operators.
  • Membership operators.
  • Bitwise operators.

What does 3 dots mean in Python? ›

In Short: Use the Ellipsis as a Placeholder in Python

That means you can use an ellipsis as a placeholder similar to the pass keyword. Using three dots creates minimal visual clutter. So, it can be convenient to replace irrelevant code when you're sharing parts of your code online.

Is it easy to learn Python? ›

Python is widely considered among the easiest programming languages for beginners to learn. If you're interested in learning a programming language, Python is a good place to start.

How do you read data from a file in Python? ›

Use the read() function(reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file) to read the data of the file and print it. Use the close() function to close the file after reading the binary data from the file.

How to read binary JSON file in Python? ›

The json. load() is used to read the JSON document from file and The json. loads() is used to convert the JSON String document into the Python dictionary. fp file pointer used to read a text file, binary file or a JSON file that contains a JSON document.

How do you read a file effectively in Python? ›

Reading text files in Python is relatively easy to compare with most of the other programming languages. Usually, we just use the “open()” function with reading or writing mode and then start to loop the text files line by line. This is already the best practice and it cannot be any easier ways.

How to read data from file from path in Python? ›

Steps For Opening File in Python
  1. Find the path of a file. We can open a file using both relative path and absolute path. ...
  2. Decide the access mode. ...
  3. Pass file path and access mode to the open() function. ...
  4. Read content from a file. ...
  5. Write content into the file. ...
  6. Close file after completing operation.
Jul 25, 2021

How do you read and write in binary format in Python? ›

Reading and writing binary file is done by appending b to the mode string. In Python 3, the binary data is represented using a special type called bytes . The bytes type represents an immutable sequence of numbers between 0 and 255.

What is a binary file in Python? ›

A binary file is a file whose content is in a binary format consisting of a series of sequential bytes, each of which is eight bits in length. The content must be interpreted by a program or a hardware processor that understands in advance exactly how that content is formatted and how to read the data.

How to convert binary data to JSON in Python? ›

  1. Split by newlines, JSON-decode each line separately. ...
  2. my_json = content.decode('utf8').split('\n') json_data = json.loads(my_json) when i used this, it is giving the exception: raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not list.
Oct 14, 2022

How to convert binary data to JSON? ›

The JSON format natively doesn't support binary data. The binary data has to be escaped so that it can be placed into a string element (i.e. zero or more Unicode chars in double quotes using backslash escapes) in JSON. An obvious method to escape binary data is to use Base64.

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6335

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.