[ SciPy v1.12.0 Manual  ](../../index.html)

  * [ Installing ](https://scipy.org/install/)
  * [ User Guide ](../../tutorial/index.html)
  * [ API reference ](../index.html)
  * [ Building from source ](../../building/index.html)
  * [ Development ](../../dev/index.html)
  * [ Release notes ](../../release.html)

__ __ __

1.12.0

  * [ __ GitHub  ](https://github.com/scipy/scipy "GitHub")
  * [ __ Twitter  ](https://twitter.com/SciPy_team "Twitter")

__

  * [ scipy ](../main_namespace.html)
  * [ scipy.cluster ](../cluster.html)
  * [ scipy.constants ](../constants.html)
  * [ scipy.datasets ](../datasets.html)
  * [ scipy.fft ](../fft.html)
  * [ scipy.fftpack ](../fftpack.html)
  * [ scipy.integrate ](../integrate.html)
  * [ scipy.interpolate ](../interpolate.html)
  * [ scipy.io ](../io.html)
  * [ scipy.linalg ](../linalg.html)
  * [ scipy.misc ](../misc.html)
  * [ scipy.ndimage ](../ndimage.html)
  * [ scipy.odr ](../odr.html)
  * [ scipy.optimize ](../optimize.html)
  * [ scipy.signal ](../signal.html)
  * [ scipy.sparse ](../sparse.html)
  * [ scipy.spatial ](../spatial.html)
  * [ scipy.special ](../special.html)
  * [ scipy.stats ](../stats.html)

__ On this page

  * ` FortranFile  `

#  scipy.io.FortranFile  #

_ class  _ scipy.io.  FortranFile  (  _ filename  _ , _ mode='r'  _ , _
header_dtype=<class  'numpy.uint32'> _ )  [ [source]
](https://github.com/scipy/scipy/blob/v1.12.0/scipy/io/_fortran.py#L33-L354) #

    

A file object for unformatted sequential files from Fortran code.

Parameters  :

    

**filename** file or str

    

Open file object or filename.

**mode** {ârâ, âwâ}, optional

    

Read-write mode, default is ârâ.

**header_dtype** dtype, optional

    

Data type of the header. Size and endianness must match the input/output file.

Notes

These files are broken up into records of unspecified types. The size of each
record is given at the start (although the size of this header is not
standard) and the data is written onto disk without any formatting. Fortran
compilers supporting the BACKSPACE statement will write a second copy of the
size to facilitate backwards seeking.

This class only supports files written with both sizes for the record. It also
does not support the subrecords used in Intel and gfortran compilers for
records which are greater than 2GB with a 4-byte header.

An example of an unformatted sequential file in Fortran would be written as:

    
    
    OPEN(1, FILE=myfilename, FORM='unformatted')
    
    WRITE(1) myvariable
    

Since this is a non-standard file format, whose contents depend on the
compiler and the endianness of the machine, caution is advised. Files from
gfortran 4.8.0 and gfortran 4.1.2 on x86_64 are known to work.

Consider using Fortran direct-access files or files from the newer Stream I/O,
which can be easily read by [ ` numpy.fromfile  `
](https://numpy.org/devdocs/reference/generated/numpy.fromfile.html#numpy.fromfile
"\(in NumPy v2.0.dev0\)") .

Examples

To create an unformatted sequential Fortran file:

    
    
    >>> from scipy.io import FortranFile
    >>> import numpy as np
    >>> f = FortranFile('test.unf', 'w')
    >>> f.write_record(np.array([1,2,3,4,5], dtype=np.int32))
    >>> f.write_record(np.linspace(0,1,20).reshape((5,4)).T)
    >>> f.close()
    

To read this file:

    
    
    >>> f = FortranFile('test.unf', 'r')
    >>> print(f.read_ints(np.int32))
    [1 2 3 4 5]
    >>> print(f.read_reals(float).reshape((5,4), order="F"))
    [[0.         0.05263158 0.10526316 0.15789474]
     [0.21052632 0.26315789 0.31578947 0.36842105]
     [0.42105263 0.47368421 0.52631579 0.57894737]
     [0.63157895 0.68421053 0.73684211 0.78947368]
     [0.84210526 0.89473684 0.94736842 1.        ]]
    >>> f.close()
    

Or, in Fortran:

    
    
    integer :: a(5), i
    double precision :: b(5,4)
    open(1, file='test.unf', form='unformatted')
    read(1) a
    read(1) b
    close(1)
    write(*,*) a
    do i = 1, 5
        writ