Ask Question
5 April, 22:49

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!

+1
Answers (1)
  1. 5 April, 23:08
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers