io_pickle.py 463 B

12345678910111213141516171819202122
  1. import pickle
  2. # The name of the file where we will store the object
  3. shoplistfile = 'shoplist.data'
  4. # The list of things to buy
  5. shoplist = ['apple', 'mango', 'carrot']
  6. # Write to the file
  7. f = open(shoplistfile, 'wb')
  8. # Dump the object to a file
  9. pickle.dump(shoplist, f)
  10. f.close()
  11. # Destroy the shoplist variable
  12. del shoplist
  13. # Read back from the storage
  14. f = open(shoplistfile, 'rb')
  15. # Load the object from the file
  16. storedlist = pickle.load(f)
  17. print storedlist