Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- # --- functions ---
- def pickle_data(email_addresses):
- # Open file for binary writing (it will close automatically)
- with open('email_addresses.dat', 'wb') as email_file:
- # Pickle the dictionary and save it to a file.
- pickle.dump(email_addresses, email_file)
- def unpickle_data():
- try:
- # Open file for binary reading (it will close automatically)
- with open('email_addresses.dat', 'rb') as email_file:
- # Unpickle the dictionary and assign to variable
- email_addresses = pickle.load(email_file)
- except FileNotFoundError:
- # Catch exception when file doesn't exist
- email_addresses = {}
- return email_addresses
- # --- main ---
- # at start unpicle data
- data = unpickle_data()
- print("data:", data)
- data = {'admin@example.com': 'admin'}
- # at the end pickle data
- pickle_data(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement