Package Scientific :: Package IO :: Module TextFile :: Class TextFile
[frames] | no frames]

Class TextFile

Text files with line iteration and transparent compression

TextFile instances can be used like normal file objects (i.e. by calling read(), 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).

Instance Methods
 
__del__(self)
 
__getitem__(self, item)
 
__init__(self, filename, mode='r')
 
close(self)
 
flush(self)
 
read(self, size=-1)
 
readline(self)
 
readlines(self)
 
write(self, data)
 
writelines(self, list)
Method Details

__init__(self, filename, mode='r')
(Constructor)

 
Parameters:
  • filename (str) - file name or URL
  • mode (str) - file access mode: 'r' (read), 'w' (write), or 'a' (append)