How to write List Object to a File??

Steve Holden sholden at holdenweb.com
Wed Apr 25 13:56:27 EDT 2001


"Juan Huertas" <jhg at galdon.com> wrote in message
news:9c667c$os8$1 at talia.mad.ttd.net...
> The best form of do this is to use pickle or cpickle.
>
This is true, but the original request explicitly ruled that out.

If the lists are simple enough, and do not contain user-provided data which
might be used to subvert your system, you could think about [untested]:


lst = ['1', '2', 'spanish', 'inquisition']
f = open(myfile, "w")
f.write(f, repr(lst))
f.close()
lst1 = eval(open(myfile, "r").read())

You should be aware, though, that this will NOT work if the list contains
anything other than simple values (for some value of "simple" which you
should satisfy yourself that your data meets).

regards
 Steve





More information about the Python-list mailing list