-6

I am still a beginner with python and xml. The following error is bothering me too much. The following is the traceback mess that I get (an error) while executing the following code:

Traceback (most recent call last):
  File "path", line 66, in <module>
    print slidename[a].childNodes[0].nodeValue
IndexError: list index out of range

from this code:

for object in os.listdir(my_directory):
      print (object)
      if '.zip' in object:
            #print (object)
            new = os.path.splitext(object)
            #print (new[0])
            file_name.append(new[0])
            os.mkdir(my_directory+new[0], 0o777)
            with zipfile.ZipFile(my_directory+new[0]+new[1], "r") as z:
                z.extractall(my_directory+new[0])

count = 0

for len_num in range(0,len(file_name)):

    for doc in os.listdir(my_directory+file_name[len_num]+new_directory):
            if '.xml' in doc:
                  #print doc
                  slide_name = os.path.splitext(doc)
                  #print slide_name
                  print ((file_name)[len_num])+'>>>'+((slide_name)[0])
                  file= xml.dom.minidom.parse(my_directory+((file_name)[len_num])+new_directory+doc)
                  tagname = file.getElementsByTagName('a:t')
                  #print tagname

                  for a in range(0,len(tagname)):
                        print tagname[a].childNodes[0].nodeValue

Thanks in advance!!

12
  • Sorry, we don't do 'urgent' here at Stack Overflow. Can you include a full traceback for that exception? Commented Apr 22, 2013 at 9:01
  • where does that error refer to? Commented Apr 22, 2013 at 9:02
  • My guess is that tagname[a].childNodes[0] throws the exception. But that is just a guess, because we have too little information here to reproduce the error. Commented Apr 22, 2013 at 9:02
  • 1
    You don't need to "think" what part of the code causes the error. The Python interpreter tells you exactly what line the error was on. Commented Apr 22, 2013 at 9:21
  • 1
    @Sangamesh: I didn't ask for your complete code. I asked for your complete traceback. The error you get comes with a details about the call stack, with source lines. We want that information. Commented Apr 22, 2013 at 9:24

1 Answer 1

3

You are parsing XML that doesn't have child nodes for that specific slidename[a] element. You probably want to skip that one.

You are not using Python loops to their full potential. There is no need to loop over indices when you can loop directly over the list itself:

for a in tagname:
    try:
        print a.childNodes[0].nodeValue
    except IndexError:
        print 'No childNodes for this element'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.