Class - 12 CBSE Computer Science Important File Handling Questions 2025
Write a method in Python to write multiple line of text contents into a text file "mylife.txt".
Python Data Handling
2 Likes
Answer
def write_to_file(file_path):
lines_to_write = ["The sun sets over the horizon.", "Birds chirp in the morning.", "Raindrops patter on the roof.", "Leaves rustle in the breeze."]
with open(file_path, "w") as file:
for line in lines_to_write:
file.write(line + '\n')
write_to_file("mylife.txt")
Answered By
2 Likes