Learn to Code via Tutorials on Repl.it!

← Back to all posts
Save in Python

If you want to save something in python, there are two ways you can go about it. One, is with pickle. I won't be covering that since it is a bit more complicated then the technique I use.
Ok, the first thing that you need to do is create a python file. Then a .txt file.
I named mine ‘save.txt’
In the python file, define the variables that you would like to save:

Alrighty, now we need to save these to the file. Define a function called ‘save()’

Ok, so now we have our information in a file, how do we get it out? First, define a function called ‘load()’

Now, we need to test if this works. First, we’ll set the variables to an input:

We’ll input Bob for name, then Python, 45, and pig.
Then we’ll save the data:

Then load it:

Next we’ll check if it actually saved the data, we’ll print it out:


So there you have it, a way to save a list of variables into a text file.
I hope you learned something, and if you didn’t, throw some rotten fruit then I hope you enjoyed reading this.
And if you want to see the repl, you can check it out here: https://repl.it/@forthethethethe/Saving-to-TXT#main.py


Would this work for more than one save? Or would I need to do something else?


bu- bu- json!