[Tutor] Structured files?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 2 09:58:23 CEST 2011


"Válas Péter" <sulinet at postafiok.hu> wrote
> I can create files and write strings/unicodes.
> Is it possible to write a list, a dictionary or an object or 
> anything into a
> file? Or do I have to transform them to strings?

You can write anything to a file, but the file will need to
be opened in binary mode(see the docs). The tricky bit
is reading the data back, you will need to find a way
to convert the bytes read from the file into whatever
the original data structure was.

For that reason it is usually better to use a tool like
pickle or shelve to store Python objects in a file
(again see the docs)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list