Output and Redirection - Page 12
May 30, 2001
""" myIO: module, (contains functions CaptureOutput,
RestoreOutput, PrintFile, and ClearFile )"""
import sys
_fileObject = None
def CaptureOutput( file="captureFile.txt"):
""" CaptureOutput( file='captureFile.txt'): redirect the
standard output to 'file'."""
global _fileObject
print "output will be sent to file: %s" % (file)
print "restore to normal by calling 'mu.RestoreOutput() '"
_fileObject= open( file, 'w')
sys.stdout = _fileObject
def RestoreOutput():
""" RestoreOutput(): restore the standard output back to
the default (also closes the capture file)"""
global _fileObject
sys. stdout = sys.__ stdout__
_fileObject. close()
print "standard output has been restored back to normal"
def PrintFile( file="captureFile.txt"):
""" PrintFile( file="captureFile.txt"): print the given file
to the standard output"""
f = open( file, 'r')
print f.read()
def ClearFile( file="captureFile.txt"):
""" ClearFile( file="captureFile.txt"): clears the contents
of the given file"""
f = open( file, 'w')
f. close()
The Struct Module
Generally speaking, when working with your own files, you
probably don't want to read or write binary data in Python. For
very simple storage needs, it is usually best to use textual
input and output as described above. For more sophisticated
applications, Python provides the ability to easily read or
write arbitrary Python objects, pickling, described later in
this chapter. This ability is much less error-prone than
directly writing and reading your own binary data, and so is
highly recommended.
However, there is at least one situation in which you will
likely need to know how to read or write binary data, and that
is when dealing with files which are generated or used by other
programs. This section gives a short description of how to do
this using the struct module. Refer to the Python reference
documentation for more details.
Python does not actually support explicit binary input or
output. Instead, in keeping with its philosophy of
modularization, it simply reads and writes strings, which are
really just byte sequences, and provides the standard
struct module to permit you to treat those strings
as formatted byte sequences with some specific meaning.
Assume that we wish to read in a binary file called
data, containing a series of records generated by a
C program. Each record consists of a C short integer, a C double
float, and a sequence of four characters that should be taken as
a four-character string. We wish to read this data into a Python
list of tuples, with each tuple containing an integer, floating-
point number, and a string.
The first thing to do is to define a format string
understandable to the struct module, which
tells the module how the data in one of our records is packed.
The format string uses characters meaningful to
struct to indicate what type of data is expected
where in a record. For example, the character 'h'
indicates the presence of a single C short integer, and the
character 'd' indicates the presence of a single C
double-precision floating-point number. Not surprisingly,
's' indicates the presence of a string, and may be
preceded by an integer to indicate the length of the string;
'4s' indicates a string consisting of four
characters. For our records, the appropriate format string is
therefore 'hd4s'.struct understands a wide range of
numeric, character, and string formats. See the Python
Library Reference for details.
Before we start reading records from our file, we need to know
how many bytes to read at a time. Fortunately,
struct includes a calcsize function,
which simply takes our format string as argument and returns the
number of bytes used to contain data in such a format.
To read each record, we will simply use the read
method described previously. Then, the
struct.unpack function conveniently returns a tuple
of values by parsing a read record according to our format
string. The program to read our binary data file is remarkably
simple:
import struct
recordFormat = 'hd4s'
recordSize = struct. calcsize( recordFormat)
resultList = []
input = open(" data", 'rb')
while 1:
# Read in a single record.
record = input. read( recordSize)
# If the record is empty, it indicates we have
#reached the end of file, so quit the loop.
# Note that we have made no provision for checking
# for file consistency,
# i. e. that the file contains a number of bytes which
# is an integer multiple
# of the record size. However, if the last record is an
# "odd" size, the
# struct. unpack function will raise an error. if
# record == '':
input. close()
break
# Unpack the record into a tuple, and append that
# tuple to the result list.
resultList. append( struct. unpack( recordFormat, record))
As you might already have guessed, struct also
provides the ability to take Python values and convert them into
packed byte sequences. This is accomplished through the
struct.pack function, which is almost, but not
quite, an inverse of struct.unpack. The "almost"
comes from the fact that while struct.unpack
returns a tuple of Python values, struct.pack does
not take a tuple of Python values; rather, it takes a format
string as its first argument, and then enough additional
arguments to satisfy the format string. So, to produce a binary
record of the form used in the above example, we might do
something like this:
>>> import struct
>>> recordFormat = 'hd4s'
>>> struct. pack( recordFormat, 7, 3.14, 'gbye')
'\007\000\000\000\000\000\000\
000\037\205\353Q\270\036\011@gbye'
[Lines 4 and 5 above are one line. They have been split for
formatting purposes.]
struct gets even better than this; you can insert
other special characters into the format string to indicate
that data should be read/ written in big-endian, little-endian,
or machine-native-endian format (default is machine-native), and
to indicate that sizes of things like C short integer should
either be sized as native to the machine (the default), or as
standard C sizes. But, if you need these features, it's nice to
know they exist. See the Python Library Reference for
details.
Screen Input/Output and Redirection - Page 11
The Quick Python Book
Pickling Objects Into Files - Page 13
|