Parsing xml file in python

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Oct 30 14:59:35 EDT 2007


On Tue, 30 Oct 2007 11:45:17 -0700, amjadcsu wrote:

> I am not getting into the xml parser.

What does this mean!?

> The error is not generated but also the /root/yhpc-2.0/ganglia.txt does
> not contain anything.

Maybe because…

>> >     def endElement(self,name):
>> >         if name=="HOST" and self.searchTerm==self.hostname:
>> >             try:
>> >                 fh=open('/root/yhpc-2.0/ganglia.txt' ,'w')
>> >             except:
>> >                 print "File /root/yhpc-2.0/ganglia.txt can not be
>> > open"
>> >                 sys.exit(1)
>> >             fh.write("This is a test for xml parsing with python with
>> > chris and amjad \n")
>> >             fh.write("the host name is", self.hostname, "\n")

…this line will raise an exception.  `file.write()` takes just one
argument, not three as in this call.  If you don't get an exception maybe
you have other places with a bare ``except`` like in the snippet above. 
Don't do that.  Catch the specific exception you want to handle with an
``except`` and not simply *all*.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list