Computer Science
Write a method in Python to write multiple line of text contents into a text file "mylife.txt".
Python Data Handling
1 Like
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
1 Like
Related Questions
Write a function Remove_Lowercase() that accepts two file names, and copies all lines that do not start with lowercase letter from the first file into the second file.
Write a program to display all the records in a file along with line/record number.
Write a method in Python to read the content from a text file diary.txt line by line and display the same on screen.
Write appropriate statements to do the following:
(a) To open a file named "RESULT.DAT" for output.
(b) To go to the end of the file at any time.