Check sibling questions

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

Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class


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

Ask a doubt
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 13 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.