Python Forum
How to save and load data from files using pickle - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/Forum-General)
+--- Forum: Tutorials (https://python-forum.io/Forum-Tutorials)
+---- Forum: Tutorial Requests and Submissions (https://python-forum.io/Forum-Tutorial-Requests-and-Submissions)
+---- Thread: How to save and load data from files using pickle (/Thread-How-to-save-and-load-data-from-files-using-pickle)



How to save and load data from files using pickle - SheeppOSU - Jun-12-2019

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


RE: How to save and load data from files using pickle - metulburr - Jun-12-2019

https://www.benfrederickson.com/dont-pickle-your-data/


RE: How to save and load data from files using pickle - micseydel - Jun-12-2019

And so that it's in this thread - JSON covers a huge fraction of serialization needs.