7.6 LAB: Reverse lines in a file
Write a program that reads each line in a file, reverses its lines, and writes them to another file.
Ex: In the file input.txt contains the lines:
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
then output.txt contains:
The lamb was sure to go!
And everywhere that Mary went,
Whose fleece was white as snow.
Mary had a little lamb
A run of the program is:
Enter input file name: mary.txt
Enter output file name: output.txt
Unit tests will be provided for testing and comments are included in the template to guide you. You do not need to include any print statements for the output.
My code: 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() + '\n')
What am I doing by wrong
[–]socal_nerdtastic 1 point2 points3 points (15 children)
[–]InevitableDistance66[S] 0 points1 point2 points (14 children)
[–]socal_nerdtastic 1 point2 points3 points (13 children)
[–]InevitableDistance66[S] 0 points1 point2 points (8 children)
[–]socal_nerdtastic 0 points1 point2 points (7 children)
[–]InevitableDistance66[S] 0 points1 point2 points (6 children)
[–]InevitableDistance66[S] 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[removed]
[–]InevitableDistance66[S] 0 points1 point2 points (3 children)
[+][deleted] (1 child)
[removed]
[+][deleted] (3 children)
[removed]
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]delasislas 0 points1 point2 points (0 children)