Writing Files

In order to write to files they need to be opened in either write mode or append mode. Use the write() method to write or append to the file.

Example:

i>>> baconFile = open('bacon.txt', 'w')
i>>> baconFile.write('Hello, world!\n')
14
i>>> baconFile.close()
i>>> baconFile = open('bacon.txt', 'a')
i>>> baconFile.write('Bacon is not a vegetable.')
25
i>>> baconFile.close()
i>>> baconFile = open('bacon.txt')
i>>> content = baconFile.read()
i>>> baconFile.close()
i>>> print(content)
Hello, world!
Bacon is not a vegetable.