. Write a program that reads each line in a file, reverses its lines, and writes
ID: 3754904 • Letter: #
Question
. Write a program that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had a little lamb Name your Python file: reverse.py
WRITE IN PYTHON
Explanation / Answer
with open('input.txt', 'r') as f, open('output.txt', 'w') as w: lines = [] for line in f: lines.append(line.strip()) while len(lines) > 0: w.write(lines.pop() + ' ')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.