Write a method in Python to write multiple lines of text contents into a text file, ''MYFILE.TXT'' line.

 

Answer:

def write():

f=open("MYFILE.txt",'w')

while True:

line=input("Enter a line: ")

f.write(line)

more_line=input("Are there more lines? Enter Y for yes and N for no: ")

if more_line == 'N':

break

f.close()

Question 5-Write a method in Python - Teachoo.JPG

Go Ad-free

Transcript

Question 5 Write a method in Python to write multiple lines of text contents into a text file, ''MYFILE.TXT'' line. declaring function count( ) Taking a user input as ‘line’ for writing it to the file. Invoking function write( ) opening the text file in read mode and assigning it to file_object ‘f’ Writes ‘line’ to the text file. Output

Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo