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

Remove Ads

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 is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.