Now that we’ve learned how to open and read a file, let’s try writing to it. To do that, we’ll use the ‘a’ (append) mode. You can also use the ‘w’ mode, but you’ll erase all previous content in the file if the file already exists. Try running the following program.
f = open (‘myfile.txt’, 'a')
f.write(‘\nThis sentence will be appended.’)
f.write(‘\nPython is Fun!’)
f.close()
Here we use the write() function to append the two sentences ‘This sentence will be appended.’ and ‘Python is Fun!’ to the file, each starting on a new line since we used the escape characters ‘\n’. You’ll get
Learn Python in One Day and Learn It Well
Python for Beginners with Hands-on Project
The only book you need to start coding in Python immediately
http://www.learncodingfast.com/python
This sentence will be appended.
Python is Fun!