Python read unformatted binary file

Use nupy.fromfile (http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html)

I guess you have missed something in fortran code, to write to binary file apply this code:

program test
implicit none
integer i,j,k,l, reclen
real*4       pcp(2,3,4)

inquire(iolength=reclen)pcp(:,:,1)
open(10, file='pcp.bin', form='unformatted', access = 'direct', recl = reclen)
pcp = 0
l = 0
do i=1,2
do j=1,2
do k=1,2
   print*,i,j,k,k+l*2
   pcp(i,j,k)=k+l*2
   l = l + 1
enddo
enddo
enddo
do k=1,4
   write(10, rec=k)pcp(:,:,k)
enddo
close(10)
end

To read file by python:

import numpy as np
with open('pcp.bin','rb') as f:
    for k in xrange(4):
        data = np.fromfile(f, dtype=np.float32, count = 2*3)
        print np.reshape(data,(2,3))

Output:

[[  1.   9.   5.]
 [ 13.   0.   0.]]
[[  4.  12.   8.]
 [ 16.   0.   0.]]
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
[[ 0.  0.  0.]
 [ 0.  0.  0.]]

read unformatted / binary fortran file with python

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

program test
integer :: i
open(1, file='test.dat', access='stream', form='unformatted')
do i = 1, 10
write(1) i
write(1) dble(i)**2
end do
close(1)
end program test

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import numpy as np
f = open('test.dat', 'rb')
dt = np.dtype("i4,f8")
x = np.fromfile( f, dtype=dt, count=-1 )
f.close()
print x

The file that contains the binary data is called a binary file. Any formatted or unformatted binary data is stored in a binary file, and this file is not human-readable and is used by the computer directly.  When a binary file is required to read or transfer from one location to another location, the file’s content is converted or encoded into a human-readable format. The extension of the binary file is .bin. The content of the binary file can be read by using a built-in function or module. Different ways to read binary files in Python have been shown in this tutorial.

Pre-requisite:

Before checking the examples of this tutorial, it is better to create one or more binary files to use in the example script. The script of two python files has given below to create two binary files. The binary1.py will create a binary file named string.bin that will contain string data, and the binary2.py will create a binary file named number_list.bin that will contain a list of numeric data.

Binary1.py

# Open a file handler to create a binary file

file_handler = open("string.bin", "wb")

# Add two lines of text in the binary file

file_handler.write(b"Welcome to LinuxHint.\nLearn Python Programming.")

# Close the file handler

file_handler.close()

Binary2.py

# Open a file handler to create a binary file

file=open("number_list.bin","wb")

# Declare a list of numeric values

numbers=[10,30,45,60,70,85,99]

# Convert the list to array

barray=bytearray(numbers)

# Write array into the file

file.write(barray)

file.close()

Example-1: Read the binary file of string data into the byte array

Many ways exist in Python to read the binary file. You can read the particular number of bytes or the full content of the binary file at a time. Create a python file with the following script. The open() function has used to open the string.bin for reading. The read() function has been used to read 7 characters from the file in each iteration of while loop and print. Next, the read() function has been used without any argument to read the full content of the binary file that will be printed later.

# Open the binary file for reading

file_handler = open("string.bin", "rb")

# Read the first three bytes from the binary file

data_byte = file_handler.read(7)

print("Print three characters in each iteration:")

# Iterate the loop to read the remaining part of the file

while data_byte:

    print(data_byte)

    data_byte = file_handler.read(7)

# Read the entire file as a single byte string

with open('string.bin', 'rb') as fh:

    content = fh.read()

print("Print the full content of the binary file:")

print(content)

Output:

The following output will appear after executing the above script.

Python read unformatted binary file

Example-2: Read the binary file of string data into the array

Create a python file with the following script to read a binary file named number_list.bin created previously. This binary file contains a list of numeric data. Like the previous example, the open() function has used open the binary file for reading in the script. Next, the first 5 numbers will be read from the binary file and converted into a list before printing.

# Open the binary file for reading

file = open("number_list.bin", "rb")

# Read the first five numbers into a list

number = list(file.read(5))

# Print the list

print(number)

# Close the file

file.close()

Output:

The following output will appear after executing the above script. The binary file contains 7 numbers, and the first five numbers have printed in the output.

Python read unformatted binary file

Example-3: Read binary file using NumPy

The ways to create the binary file using the NumPy array and read the content of the binary file using into a list by using the NumPy module have shown in this part of the tutorial. Before checking the script given below, you have to install the NumPy module by executing the command from the terminal or installing the NumPy package in the Python editor, where the script will be executed. The tofile() function is used to create a text or binary file, and the fromfile() function is used to create an array by reading a text or binary file.

Syntax of tofile():

ndarray.tofile(file, sep='', format='%s')

The first argument is mandatory and takes the filename or string or path as a value. The file will be created if a filename is provided in this argument. The second argument is optional that is used to separate the array elements. The third argument is optional also and used for formatting the output of the text file.

Syntax of fomfile():

numpy.fromfile(file, dtype=float, count=- 1, sep='', offset=0, *, like=None)

The first argument is mandatory and takes the filename or string or path as a value. The content of the file will be read if a filename will be provided in this argument. The dtype defines the data type of the returned array. The count is used to count the number of items. The purpose of the sep is to separate the text or array items. The offset is used to define the current position of the file. The last argument is used to create an array object that not a NumPy array.

Create a python file with the following script to create a binary file using NumPy array and read and print the content of the binary file.

# Import NumPy module

import numpy as np

# Declare numpy array

nparray = np.array([34, 89, 30, 45, 90, 11])

# Create binary file from numpy array

nparray.tofile("list.bin")

# Print data from the binary file

print(np.fromfile("list.bin",  dtype=np.int64))

Output:

The following output will appear after executing the above script.

Python read unformatted binary file

Conclusion:

Three different ways to read the binary file have been shown in this tutorial by using simple examples. The first example returned the content of the binary file as a byte array. The second example returned the content of the binary file as a list. The last example also returned the content of the binary file as a list.

About the author

Python read unformatted binary file

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

How do I read 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. ... .
For large binary files, you can use the Read method of the FileStream object to read from the file only a specified amount at a time..

How do I read a Fortran binary file in Python?

>>> from scipy.io import FortranFile >>> f = FortranFile( 'pcp. bin', 'r' ) >>> print( f. read_reals( dtype='float32' ) ) [ 1.

How do I run a binary file in Python?

Python read a binary file line by line.
In this example, I have taken a line as lines=[“Welcome to python guides\n”] and open a file named as file=open(“document1. txt”,”wb”) document1. ... .
The “wb” is the mode used to write the binary files. The file. ... .
The writelines() returns the sequence of string to the file. The file..

Can Python read binary files?

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.