Best way to print a module?

Martin De Kauwe mdekauwe at gmail.com
Mon Sep 5 19:20:42 EDT 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