Here is the main procedure of the script.
# - - - m a i n - - -
def main():
'''Display the structure of a .dbf file.
'''
The command line should contain exactly one argument, the
name of the .dbf file to be displayed.
#-- 1 --
# [ if sys.argv contains exactly one argument ->
# fileName := that argument
# else ->
# sys.stderr +:= error message
# stop execution ]
argList = sys.argv[1:]
if len(argList) != 1:
print >>sys.stderr, "*** One argument, the .dbf file name."
sys.exit(1)
else:
fileName = argList[0]
Next we attempt to open the input file. Any sort of
exception here is fatal. See Section 90.1, “Dbf: Database object”.
#-- 2 --
# [ if fileName names a readable, valid .dbf file ->
# table := a dbf.Dbf object representing that file
# header := a dbf.DbfHeader object representing the
# header data from that file
# else ->
# sys.stderr +:= error message
# stop execution ]
try:
table = Dbf(fileName)
header = table.header
except Exception, detail:
print >>sys.stderr,("*** Couldn't open the input file: %s" %
detail)
sys.exit(2)
Since the order of the records isn't important, we can
use the default iteration over the Dbf
object, which returns each record as a DbfRecord object. See Section 89.3, “flattenRecord(): Flatten one record”.
#-- 3 --
# [ sys.stdout +:= records from table, flattened ]
for record in table:
#-- 3 body --
# [ (header is the DbfHeader object defining the
# format of the file) and
# (record is a record from that file as a DbfRecord object) ->
# sys.stdout +:= record, with each field padded
# to the length for that field in header ]
flattenRecord(header, record)