Best way to print a module?

Martin De Kauwe mdekauwe at gmail.com
Tue Sep 6 01:20:42 CEST 2011


Trying to follow the suggestion this would be the alternate
implementation.

import sys
sys.path.append("/Users/mdekauwe/Desktop/")
import params

#params.py contains
#apples = 12.0
#cats = 14.0
#dogs = 1.3

fname = "test.asc"
try:
    ofile = open(fname, 'w')
except IOError:
    raise IOError("Can't open %s file for write" % fname)

attributes = [attr for attr in dir(params) if not
attr.startswith('__')]
attributes.sort()

try:
    ofile.write("[params]\n")
    for i in attributes:
        ofile.write("%s = %s\n" % (i, getattr(params, i)))
except IOError:
    raise IOError("Error writing params files, params section")

Is that a better version? I honestly don't know.

thanks



More information about the Python-list mailing list