Advertisement
MrLunk

Reading and Writing Lists to a File in Python

Nov 3rd, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. # Reading and Writing Lists to a File in Python
  2.  
  3. import pickle
  4. import random
  5.  
  6. # create empty list
  7. canna_list = ['a',1,'b',2,'c',3,'d',4,'e',5,'f',6,'g',7,'h',8,'g',9,'i',10,'j',11,'k',12,'l',13,'m',14]
  8.  
  9. print ('saving: ')
  10. print (canna_list)
  11.  
  12. with open("canna_list.txt", "wb") as fp:   #Pickling
  13.     pickle.dump(canna_list, fp)
  14.  
  15. with open("canna_list.txt", "rb") as fp:   # Unpickling
  16.     b = pickle.load(fp)
  17.  
  18. print ('loaded: ')
  19. print (b)
  20.  
  21. # https://www.facebook.com/mrlunk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement