Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I find it particularly difficult reading the binary file with Python. Can you give me a hand? I need to read this file, which in Fortran 90 is easily read by

int*4 n_particles, n_groups 

real*4 group_id(n_particles) 

read (*) n_particles, n_groups 

read (*) (group_id(j),j=1,n_particles)

In detail, the file format is:

Bytes 1-4 -- The integer 8. 

Bytes 5-8 -- The number of particles, N. 

Bytes 9-12 -- The number of groups. 

Bytes 13-16 -- The integer 8. 

Bytes 17-20 -- The integer 4*N. 

Next many bytes -- The group ID numbers for all the particles. Last 4 bytes -- The integer 4*N.

How can I read this with Python? I tried everything but it never worked. Is there any chance I might use an f90 program in python, reading this binary file and then save the data that I need to use?

1 Answer

0 votes
by (106k points)

To read the binary file content you can use the below-mentioned code:-

with open(fileName, mode='rb') as file: 

fileContent = file.read()

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...