Write variable to File
How Can We Help?
Teacher,
Cat enthusiast
- The 2022 End of Year Website Update - 01/01/2023
- Celebrating Ada Lovelace day - 10/10/2022
- Website update 5/9/22 - 05/09/2022
This block will write whatever is stored in a variable to a text file.
You can name the text (.txt) file anything you like by changing what is written between the speech marks. You must however write .txt after your file name EG – myfilename.txt
You can write any variable you want to the text file by selecting the variable from the drop down list on the second line.
Example
We have created a variable that we want to print called hello_text. Inside this variable is a message that says Hello Bob.
We have called the file that we want to write to readme.txt
We are writing the variable hello_text into the text file.
When we run this code in repl or any other IDE we will not see anything on the console screen. Instead we should see our text file being created in the same folder as our Python file.
Our text file should contain the value from the variable.