Export variables to files or Import files
-
Introduction
For every variable in a project, you can write code to export it to a text file on the user’s computer, or import a file from the user’s computer into a variable. This is useful when you need to allow the user to save some data that can be used later. For example, when you program a game, you may want to allow the players to save their current progress into a file and reload it later.
Export a Variable
You can use the “export variable [VARIABLE NAME]” block to export the value of a variable to a text file. For example, when you run this program, a new file named “variable1” will be downloaded into the “Downloads” folder on the user’s computer, and its content will be “22”. Of course, you can store much more data in a variable, and it will all be saved into the file.
Import a File into a Variable
You can also import a file into a variable like this:
When the user runs this program, an “Open File” dialog window will pop up, and the user can select ANY file that ends with “.txt” or “.csv”. Note that the file name doesn’t have to be the same as the variable name. Once the user selects a file, that file’s content will be stored in the variable you specified. If the user has cancelled the dialog window without selecting any file, then the variable’s content will not be changed.