Dumping and Loading Objects Using Pickle

import pickle

def main():
    outfile = open("pickle.dat", "wb")
    pickle.dump(45, outfile)
    pickle.dump(56.6, outfile)
    pickle.dump("Programming is fun", outfile)
    pickle.dump([1,2,3,4], outfile)
    outfile.close()

    infile = open("pickle.dat", "rb")
    print(pickle.load(infile))
    print(pickle.load(infile))
    print(pickle.load(infile))
    print(pickle.load(infile))
    infile.close()

main()


detect end of file

import pickle


def main():
    outfile = open("numbers.dat", "wb")

    data = eval(input("Enter an integer (the input exits of the input is 0):"))
    while data != 0:
        pickle.dump(data, outfile)
        data = eval(input("Enter an integer (the input exits of the input is 0):"))

    outfile.close()

    infile = open("numbers.dat", "rb")

    end_of_file = False

    while not end_of_file:
        try:
            print(pickle.load(infile), end = " ")
        except EOFError:
            end_of_file = True

    infile.close()

    print("\nAll objects are read")

main()