Ask Question
29 February, 17:25

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!

+2
Answers (1)
  1. 29 February, 20:10
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers