2. Write a program that reads each line in a file, reverses its lines, and write
ID: 3722014 • Letter: 2
Question
2. Write a program that reads each line in a file, reverses its lines, and writes them to another file. For example, the previous file lamb.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! and you run reverse lamb.txt to out lamb.tx then out lamb.txt contains The lamb was sure to go. And everywhere that Mary went Whose fleece was white as snow Mary had a liule lamb lint: You might use loop or simply readline() and write) methodsExplanation / Answer
file=open("lamb.txt","r")
file_list=file.readlines()
no_rows=len(file_list);
file_write=open("out_lamb.txt","w")
for i in range(0,no_rows,1):
file_write.write(file_list[no_rows-1-i])
file_write.close()
file.close()
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.