[Tutor] IndexError: list index out of range : f2.write(col[0] + '\t' + col[1] + '\n')

kumar s ps_python at yahoo.com
Wed Nov 3 17:12:48 CET 2004


Dear Group, 
 This is a new question about IndexError and thats the
reason I pasterd f2.write(col[0] + '\t' + col[1] +
'\n') in the subject line. 




I wrote a function to parse coluns in a tab delimited
text file. 

def cutter (f1,f2):
	file = open(f1,'r')
	file2 = open(f2,'w')
	fopen = file.read()
	flist = split(fopen,'\n')
	for i in range(len(flist)):
		col = split(flist[i],'\t')
		file2.write(col[0] + '\t' + col[1] + '\t' + col[3] +
'\n' )
		file2.close()


>>> cutter('gene_data.txt','test1.txt')

Traceback (most recent call last):
  File "<pyshell#44>", line 1, in -toplevel-
    cutter('gene_data.txt','test1.txt')
  File "<pyshell#42>", line 8, in cutter
    file2.write(col[0] + '\t' + col[1] + '\t' + col[3]
+ '\n' )
ValueError: I/O operation on closed file


Why am I getting ValueError and I/O.  Why shouldnt I
close file2.close()?



2nd Question:

I removed file2.close() line in the code. It is now
like this:
>>> def cutter (f1,f2):
	file = open(f1,'r')
	file2 = open(f2,'w')
	fopen = file.read()
	flist = split(fopen,'\n')
	for i in range(len(flist)):
		col = split(flist[i],'\t')
		file2.write(col[0] + '\t' + col[1] + '\t' + col[3] +
'\n' )

		
>>> cutter('gene_data.txt','test1.txt')

Traceback (most recent call last):
  File "<pyshell#47>", line 1, in -toplevel-
    cutter('gene_data.txt','test1.txt')
  File "<pyshell#46>", line 8, in cutter
    file2.write(col[0] + '\t' + col[1] + '\t' + col[3]
+ '\n' )
IndexError: list index out of range
>>> 



Now, why am I getting IndexError: list out of range.
What is the problem.  I got the result but an error is
not so good. Can any one help me explaing the
situation.

Thanks.

Kumar.





		
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 



More information about the Tutor mailing list