Answers for "how to save a python object in a file"

0

how to save a python object in a file

def save_object(obj, filename):
    with open(filename, 'wb') as outp:  # Overwrites any existing file.
        pickle.dump(obj, outp, pickle.HIGHEST_PROTOCOL)

# sample usage
save_object(company1, 'company1.pkl')
Posted by: Guest on October-17-2021
0

save object to file python

class client:
    def __init__(self, name, surname):
        self.name = name
        self.surname = surname
#create an instance of client        
peter = client('Peter', 'Johnnson's')
#save it to file for later access               
f = open('peterInfo.pickle', 'wb')
pickle.dump(peter, f)
f.close()
# Read an object from file
f = open('peterInfo.pickle', 'rb')
data = pickle.load(f)
print(data)
f.close()
Posted by: Guest on October-11-2021

Code answers related to "how to save a python object in a file"

Python Answers by Framework

Browse Popular Code Answers by Language