write()
Note: if the file already exists, its contents are overwritten!
E.g., we have a text file out.txt
:
This is the first line!
The program:
# open, write to, and close the file aFile = file("out.txt", "w") aFile.write("first line\nsecond line") aFile.close() # open, read from, and close the file aFile = file("out.txt", "r") print aFile.read() aFile.close()
prints:
first line second line