Writing to files

Justin Sheehy dworkin at ccs.neu.edu
Mon Mar 20 17:09:52 EST 2000


"Daley, MarkX" <markx.daley at intel.com> writes:

> Here is a piece of code I wrote to write the items in a list sequentially to
> a file.
> 
> Definitions:
> 
> a=some list ([1,2,3,whatever])
> f=open('filename','a')
> 
> 	for x in a:
> 		f.write(a[x])
> 
> When I run it, I get this:
> 
> 	Traceback (innermost last):
>  		 File "<pyshell#2>", line 1, in ?
>    		 collect.collect()
>   	File "C:\PROGRA~1\Python\collect.py", line 30, in collect
>     		f.write(a[x])
> 	TypeError: sequence index must be integer
> 
> Any ideas on what is causing this?

x is not an integer.

Perhaps instead of f.write(a[x]), you just wanted f.write(x)?

-Justin

 




More information about the Python-list mailing list