Ask Question
20 November, 12:42

Complete the given program that reads a file containing text. The program is required to read each line and send it to the output file, preceded by line numbers. If the input file contains Mary had a little lamb Whose fleece was white as snow. And everywhere that Mary went, The lamb was sure to go! then the program will produce an output file that contains the following content: / * 1 * / Mary had a little lamb / * 2 * / Whose fleece was white as snow. / * 3 * / And everywhere that Mary went, / * 4 * / The lamb was sure to go!

+4
Answers (1)
  1. 20 November, 12:54
    0
    texts = "" with open ("text. txt") as file: texts = file. readlines () with open ("output. txt", "w") as file: i = 1 for s in texts: file. write ("/*" + str (i) + "*/" + s) i + = 1

    Explanation:

    The solution code is written in Python 3.

    Firstly, create a variable texts to hold the contents read from the text file. Next, open the file stream and use readlines method to read the contents from text. txt and assign it to variable texts (Line 3-4).

    Next, open another file stream but for this time the file stream is used to output each row of the texts preceded by line number (Line 6 - 10).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Complete the given program that reads a file containing text. The program is required to read each line and send it to the output file, ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers