Page 15 -
P. 15
table of contents
persistence
4 Saving data to files
It is truly great to be able to process your file-based data.
But what happens to your data when you’re done? Of course, it’s best to save your
data to a disk file, which allows you to use it again at some later date and time. Taking
your memory-based data and storing it to disk is what persistence is all about. Python
supports all the usual tools for writing to files and also provides some cool facilities for
efficiently storing Python data.
Programs produce data 106
Open your file in write mode 110
Files are left open after an exception! 114
Extend try with finally 115
Knowing the type of error is not enough 117
Use with to work with files 120
Default formats are unsuitable for files 124
Why not modify print_lol()? 126
Pickle your data 132
Save with dump and restore with load 133
Generic file I/O with pickle is the way to go! 137
Your Python Toolbox 138
['Is this the right room for an
argument?', "No you haven't!",
'When?', "No you didn't!", "You
didn't!", 'You did not!', 'Ah!
(taking out his wallet and paying)
Just the five minutes.', 'You most
[‘Is this the right room
for an argument?’, “No
you haven’t!”, ‘When?’,
“No you didn’t!”, “You certainly did not!', "Oh no you
didn’t!”, ‘You did not!’,
‘Ah! (taking out his wallet
and paying) Just the five
minutes.’, ‘You most
certainly did not!’, “Oh didn't!", "Oh no you didn't!", "Oh
no you didn’t!”, “Oh no
you didn’t!”, “Oh look,
this isn’t an argument!”,
“No it isn’t!”, “It’s look, this isn't an argument!",
just contradiction!”,
‘It IS!’, ‘You just
contradicted me!’, ‘You
DID!’, ‘You did just
then!’, ‘(exasperated) "No it isn't!", "It's just
Oh, this is futile!!’,
‘Yes it is!’]
contradiction!", 'It IS!', 'You
just contradicted me!', 'You DID!',
'You did just then!', '(exasperated)
Oh, this is futile!!', 'Yes it
is!']
xiii