-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!!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sangamesh
  • 435
  • 3
  • 8
  • 19
  • Sorry, we don't do 'urgent' here at Stack Overflow. Can you include a *full* traceback for that exception? – Martijn Pieters Apr 22 '13 at 09:01
  • where does that error refer to? – Aswin Murugesh Apr 22 '13 at 09: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. – Martijn Pieters Apr 22 '13 at 09:02
  • You should not loop over `range()`, loop over the lists themselves directly. `for filename in file_name` would set `filename` to each element in the `file_name` list in turn. – Martijn Pieters Apr 22 '13 at 09:04
  • Thanks Martijin [for a in range(0,len(tagname)):9 print tagname[a].childNodes[0].nodeValue I think this part of the code is the cause of the error..Can u please help me fix that one..any alternative way?? – Sangamesh Apr 22 '13 at 09:16
  • 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. – interjay Apr 22 '13 at 09:21
  • @MartijnPieters http://pastebin.com/trNVjvUJ HERE IS the link to MY COMPLETE CODE for your reference!! – Sangamesh Apr 22 '13 at 09:22
  • @interjay u r right.. :) – Sangamesh Apr 22 '13 at 09:22
  • 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. – Martijn Pieters Apr 22 '13 at 09:24
  • @MartijnPietersTraceback (most recent call last): File "path", line 66, in print slidename[a].childNodes[0].nodeValue IndexError: list index out of range The following is the traceback message i got after execution... – Sangamesh Apr 22 '13 at 10:04
  • @Sangamesh: You can to [edit] your post to add that. – Martijn Pieters Apr 22 '13 at 10:04
  • @MartijnPieters I did it...Thanks a lot..I guess i will have more issues since i am starting to learn stuff and i would ask you..hope its fine with you right! :) – Sangamesh Apr 22 '13 at 13:52

1 Answers1

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'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343