Definition:
When you use the writelines() method in a program, it writes multiple lines into a file.
Before using it, open the file in "w" mode.
It deletes the old data and writes the new data into the file.
If you do not pass any parameters, an error will appear on the screen.
Syntax:
file_object.writelines(list_of_strings)
Example:
file = open("file1.txt", "w")
lines = ["Rahul Kumar\n", "Priya Sharma\n", "Sohan Khanna\n", "Lata Singh\n"]
file.writelines(lines)
file.close()
Output (file content after writelines()):
Exercise
Write a Python program using writelines() method to write multiple lines into a file.
