Write in File
Last Updated : 5th September 2025
Write a file means to write the content of a file. It remove the content of the file and write the new content.
1. write()
It writes the given content to the file.
file = open("example.txt", "w")
file.write("Hello, World!")
file = open("example.txt", "w")
file.write("Hello, World!\nThis is Python")
file.close()
2. writelines()
It writes a list of strings to the file.
file = open("example.txt", "w")
lines = ["Hello, World!\n", "This is Python"]
file.writelines(lines)
file.close()
3. flush()
It write buffer content to the file.
file = open("example.txt", "w")
file.write("Hello, World!")
file.flush()