Class fort.File

class File(fd)

A Fortran binary (unformatted) file object.

A binary Fortran file can be opened using the open method of this module; a File object is returned that supports a writeline method (for writing records), a readline method (for reading records) and the iterator protocol (for reading).

It is normally easier to use the open() factory function rather than construct instances directly.

Arguments

fd
The underlying file object, which is typically a file object opened in binary mode.

Methods

readi()

Read a single signed integer from the file and return it.

If at end-of-file then None is returned. Insufficient data (for example, only 3 bytes remaining but 4 required) will raise some sort of exception.

Return Value

Normally the integer value retrieved from the file. If end-of-file was encountered then``None`` is returned.

Exceptions Raised

FileTruncatedError
If the file did not contain a complete integer’s worth of bytes.
close()
Close the underlying file.
readline()

Read a single binary record as a string.

Return Value

Normally a string containing the bytes of the record. If end-of-file was encountered then``None`` is returned.

Exceptions Raised

FileFormatError
If the record has mismatching record length prefix and suffix markers.
FileTruncatedError
If the file did not apparently contain a complete record.
writeline(record)

Write a single record to the Fortran binary file.

Arguments

record
A string containing the bytes for the record.
next()

Provides itertator protocol support.

This is not normally invoked directly. It is more common to use the idiom

1
2
for record in myFile:
    ...

Attributes

fd
Underlying file descriptor. Usually returned by builtin open, but any object with a suitable read-like interface should do.
w
The number of bytes in the natural word size used in the file.
bos
Byte Order and Size. Specifies the byte order and sizes used in the underlying file. This is used in a Python struct format so we use the same convention. See http://docs.python.org/lib/module-struct.html ‘<’ little-endian, ‘>’ big-endian.