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 you close the terminal or terminate the notebook that contains our program output. This will allow us to reuse the content later with the read()
method that was covered in the previous section, Reading Files.
You will still be using the open()
method to write to a file, except 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+")
The preceding code snippet allows us to open a file in w+
, a mode that supports both reading and writing, that is updating the file. Other modes in Python include the...