Error: IndexError:list out of range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • b10803022
    New Member
    • Apr 2012
    • 1

    Error: IndexError:list out of range

    when i am executing the following code i m getting IndexError:list out of range

    Code:
    import os;
    import sys;
    file1=open(sys.argv[1],"r").readlines() # 1_train.txt
    file2=open(sys.argv[2],"w")
    for i in range(0, len(file1)):
    	w=file1[i].split();
     	if(len(w)==3):
    		file2.write(w[0]+"_"+w[2]+"\t"+w[1]+"\n");
    	else:
    		file2.write("\n");
    Last edited by Meetee; Apr 23 '12, 09:02 AM. Reason: code tags added
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It would be helpful if you posted the complete traceback. My initial thought is you only gave it one argument. You can add print statements at strategic places to see what is taking place.

    It would be more concise to iterate on the file object instead of a range. Something like:
    Code:
    file1=open(sys.argv[1],"r")
    for line in f:
        w=line.split()
        ................

    Comment

    Working...