Python Forum

Full Version: How to save and load data from files using pickle
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pickle is a great way to save and load files because it encode the data before storing it. Especially numbers. Before we use pickle we need a way to grab the current_directory the file we are working in is inside. For this we use pathlib.Path -
from pathlib import Path
current_directory = Path(__file__).parent
Excellent. For this tutorial I will be using a txt file. In order to get through directories I am going to use os -
from pathlib import Path
import os
current_directory = Path(__file__).parent
file = os.path.join(current_directory, 'DataFolder', 'Data.txt') #os.path.join joins together all the strings to return a path
Now it is time to use pickle. The two modules we are using from pickle are pickle.dump (save) and pickle.load (load). Here is pickle.save in actions -
from pathlib import Path
import os, pickle
current_directory = Path(__file__).parent #Get current directory
file = open(os.path.join(current_directory, 'DataFolder', 'Data.txt'), 'wb') #wb = write bytes because we are saving or writing
dataToSave = [1, 'foo', '2', 43]

pickle.dump(dataToSave, file) #We first give it the data to save, then the file to save it to
The pickle.load module works similarly. Here is how it works -
from pathlib import Path
import os, pickle
current_directory = Path(__file__).parent #Get current directory
file = open(os.path.join(current_directory, 'DataFolder', 'Data.txt'), 'rb') #rb = read bytes because we are reading the file

loadedData = pickle.load(file) #We give it the file and it loads in everything from the file. That data is the loaded as the variable loadedData

Hope there aren't any mistakes in there
And so that it's in this thread - JSON covers a huge fraction of serialization needs.