5. (25 XP) Write a program that reads each line in a file, reverses its lines, a
ID: 3699229 • Letter: 5
Question
5. (25 XP) Write a program that reads each line in a file, reverses its lines, and writes them to another file. Use the file named sonnet.txt containing Acquainted with the night by Robert Frost (below). Output the result to a file named re verse sonnet.txt Acquainted With The Night I have been one acquainted with the night I have walked out in rain and back in rain. I have outwalked the furthest city light I have looked down the saddest city lane. I have passed by the watchman on his beat And dropped my eyes, unvilling to explain. I have stood still and stopped the sound of feet When far avay an interrupted cry Came over houses from another street, But not to call me back or say goodby; And further stil1 at an unearthly height, One luminary clock against the sky Proclaimed the time was neither wrong nor right. I have been one acquainted with the night. by Robert Frost nverts all words to lowercase,Explanation / Answer
#Code
lines = []
with open('sonnet.txt') as f:
lines = f.readlines()
with open('reversed_sonnet.txt', 'w') as f:
for line in reversed(lines):
f.write(line)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.