Module Scientific.IO.TextFile


Class TextFile: Text files with line iteration and transparent compression

TextFile instances can be used like normal file objects (i.e. by calling readline(), readlines(), and write()), but can also be used as sequences of lines in for-loops.

TextFile objects also handle compression transparently. i.e. it is possible to read lines from a compressed text file as if it were not compressed. Compression is deduced from the file name suffixes .Z (compress/uncompress), .gz (gzip/gunzip), and .bz2 (bzip2).

Finally, TextFile objects accept file names that start with ~ or ~user to indicate a home directory, as well as URLs (for reading only).

Constructor: TextFile(filename, mode="r"), where filename is the name of the file (or a URL) and mode is one of "r" (read), "w" (write) or "a" (append, not supported for .Z files).