Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
25

Python 3: Saving data in an uneditable, unreadable file!

Muffinlavania
Muffinlavania

Introducing Pickle! This module allows you to store data in a .dat file. It saves it in binary so people cant snoop around in your files, but you can load up the file anytime you want, and it will read that binary and convert it back to english! This also means that of course its not 'unreadable', computers can translate it back. Pickle can be useful for games where you want to save your data, but also want an efficient password system! All you have to do is import pickle, then you can save, load , and add data to binary files!

Saving data:

with open(your_variable+".dat",'wb') as save: pickle.dump(any_variable, save, protocol = 2)

The pickle.dump part can be repeated as many times as you want, each one for a different variable you want to put in your newly created file!

Loading data:

global (All variables you will be loading) with open(your_variable+".dat",'rb') as save: any_variable = pickle.load(save)

The pickle.load part can again be repeated multiple times for many different variables

Adding Data!

with open(your_variable+".dat",'ab') as save: pickle.dump(any_variable, save, protocol = 2)

Of course you can do this with many variables, and 'ab' means add binary. You need to already have 'your_variable.dat' saved or the code will error

This is a little bit hard to understand, and a bit hard to remember, but it is probably the most useful module i know!

There are more notes on this in the code itself, and thanks to

@squidcoder
for first showing me this, check out his projects for many examples of pickle.

And thanks to

@medcho
for the idea of making a tutorial on this!

Edit:
If you look at the files, like HI.dat, for some stupid reason repl saves everything BUT the sensitive information in binary. I guess you can still encrypt stuff, but its weird. One good thing tho, is that i can not see any of you guy's files.(I think you guys made some right?) So like

@CodeLongAndPros
said, we are all on different servers, so no one can look at another person's file, which still makes pickle viable for passwords and other things

2 years ago
You are viewing a single comment. View All
1