Opening and writing pickled object to a file. : cPickle « Database « Python Tutorial






import sys, cPickle

try:
   file = open( "users.dat", "w" )   
except IOError, message:             
   print >> sys.stderr, "File could not be opened:", message
   sys.exit( 1 )

inputList = []

inputList.append( "A" ) 
inputList.append( "B" ) 
inputList.append( "C" ) 

cPickle.dump( inputList, file )  

file.close()

# Reading and printing pickled object in a file.

try:
   file = open( "users.dat", "r" )
except IOError:
   print >> sys.stderr, "File could not be opened"
   sys.exit( 1 )
   
records = cPickle.load( file ) 
file.close()

for record in records:         
   print record[ 0 ].ljust( 15 ),








15.1.cPickle
15.1.1.Pickling Objects to a File
15.1.2.Unpickling Objects from a File
15.1.3.Opening and writing pickled object to a file.