[SciPy-User] using numpy.fromfile without knowing the number of elements in the file

Gabriele Brambilla gb.gabrielebrambilla at gmail.com
Mon Jul 13 17:10:11 EDT 2015


Hi,

I'm trying to read an unformatted file written in Fortran.
Fortran has the problem that for each write() statement it inserts 4bytes
of "trash" at the beginning and at the end of the file.

I tried to use numpy.fromfile in this way:

import numpy as np
from math import *

f = open('../De0/SN01.dat', 'rb')

for iw in range(5):
a = np.fromfile(f , dtype = np.int8 , count = 4 )
x = np.fromfile(f , dtype = np.float64 , count = 21)
a = np.fromfile(f , dtype = np.int8 , count = 4 )
print(iw, x)


It works, but here I'm bounded to set the total number of iteration in the
FOR cycle. How can I repeat this cycle until the eof()?

Thanks

Gabriele
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150713/46f6e215/attachment.html>


More information about the SciPy-User mailing list