How to write to a file? | Python

/ #Python


In this guide, I will show you how easy it is to store information in files using Python.

# First set a variable with information
the_text = 'This information will be stored in a file'

the_file = open('the_file.txt', 'w') 
the_file.writelines(the_text) 
the_file.close() 

If you run this. A new file called "the_file.txt" should be created with the contents from the "the_text" variable.

Comments

No comments yet...

Add comment

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.