[Tutor] Trying to catch an exception...

William O'Higgins Witteman hmm at woolgathering.cx
Fri Sep 15 18:55:16 CEST 2006


I am running a program (via py2exe) that is reading some XML files with
xml.minidom.  I am getting an error wherein I pull a value from a tag
which (I think) happens to be empty.  Thus, it throws this error:

AttributeError: 'NoneType' object has no attribute 'data'

Here's the code that creates this problem:

def functionname(fileobject):
  try:
    xmldoc = minidom.parse(fileobject)
  except xml.parsers.expat.ExpatError, AttributeError:
    logit = "There is a malformed file: " + fileobject + "\n"
    logfile.write(logit)
  else:
    a = xmldoc.getElementsByTagName('date_modified')
    try:
      b = a[0].firstChild.data
    except xml.parsers.expat.ExpatError, AttributeError:
      logit = "There is a malformed file: " + fileobject + "\n"
      logfile.write(logit)

I am wondering what I have to do to catch this exception - I'm assuming
that the problem is that "a" is an empty object, and so it has not
attributes.  Thanks.
-- 

yours,

William


More information about the Tutor mailing list