In this lesson, you will learn more another basic operation — writing.
How to write to files
Let's open a file for writing, write some text, and then close it. The write
method writes a string to the file without any additional processing:
f = open("foo.txt", "w")
f.write('Hello\nWorld!') # 12
f.close()
Here "w"
signifies the write mode, and 12
indicates the number of characters we wrote to the file using the write
call. Now, let's open the file in read mode and read the entire contents: