Writing files
Now that you have learned how to read the content of a file, you are going to learn how to write content to a file. Writing content to a file is the easiest way for us to store content in our database storage, save our data by writing it to a particular file, and save data on our hard disk. This way, the output will still be available for us after we have closed the terminal or terminated the notebook that contains our program output. This will allow us to reuse the content later with the read()
method, which we covered in the previous section, Reading files.
You will still be using the open()
method to write to a file, except for when it requires an extra argument to indicate how you want to access and write to the file.
For instance, consider the following:
f = open("log.txt","w+", encoding="utf-8")
The preceding code snippet allows us to open a file in w+
, a mode that supports both reading and writing – that is, to update...