A pickle file with the pickle extension is a binary file where all the user inputs will get saved into this file in binary format. The pickle built-in module will be required in order to use the pickle’s methods to save and retrieve the content into and from the pickle file.
Below is the python program which will get the input from a user and save it into the pickle file and then displays it on the screen again after retrieving it from the pickle file. The program below will create the pickle file for you if it has not been created yet but if there already has content inside the file then the old content will get replaced by the latest one.
import pickle poet = '' try: poet = input("Enter your poet: ") with open("poet.pickle",'wb+') as f: pickle.dump(poet, f) # put poet into the pickle file except IOError as ioerror: print("Error occurred " + str(ioerror)) try: with open("poet.pickle", "rb+") as f: poet = pickle.load(f) # retrieve poet from the pickle file print(poet) except IOError as ioerror: print("Error occurred " + str(ioerror))
Now let us try it out…
Enter your poet: I am a poor man who lives inside a homeless tent! I am a poor man who lives inside a homeless tent!
Basically, a pickle file is just like a text file except it is in binary format instead of in text format.