Reputation: 946
I am trying to build a function to store data in a pickle file. I know that using pickle this way works:
with open('x.pickle', 'wb') as f:
pickle.dump(x, f)
But I actually want to make a function out of it so I don't have to write it again and again. Here is what I have tried:
def pickle_dump(x):
with open('%s.pickle'%x, 'wb') as f:
pickle.dump(x, f)
return
When we use 'with', and the file does not exist, it creates a file. I supposed that it would also work when inside a function, but instead it returned me a File Not Found Error.
Any help is appreciated.
Upvotes: 2
Views: 1520
Reputation: 40878
It looks like you're confusing the string path name with your actual object. They are two different things.
def pickle_dump(path, x):
"""Pickle object `x` to `path`."""
with open('%s.pickle' % path, 'wb') as f:
pickle.dump(x, f)
Example:
import pandas as pd
df = DataFrame() # the actual object you want to pickle
pickle_dump(path='files/filename', x=df)
To do the same thing in reverse:
def pickle_load(path):
with open('%s.pickle' %path, 'rb') as f:
return pickle.load(f)
Now to run this you would assign a variable to the returned value from the function:
result = pickle_load('mypath/filename')
In your comment, you named a variable x
in the function body. But realize that is a local variable. You can't access it from outside of the function. That's a case where you want the return statement.
Upvotes: 1
Reputation: 109546
Pass both the object and filename.
import cPickle as pickle # Python 2
def pickle_dump(obj, filename):
if filename.split('.')[-1] != 'p':
filename += '.p' # Add pickle file extension `.p` if needed.
with open(filename, 'wb') as f:
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
Upvotes: 1