Question: Read all of the instructions Instructions Write a Python program that reads each line in the file input.txt, reverses the words in each line, and

Read all of the instructions

Instructions

Write a Python program that reads each line in the file input.txt, reverses the words in each line, and writes them to output.txt.

For example, if the file input.txt contains the lines

 

Mary had a little lamb

its fleece was white as snow

and everywhere that Mary went

the lamb was sure to go

then output.txt contains the lines

 

lamb little a had Mary

snow as white was fleece its

went Mary that everywhere and

go to sure was lamb the

Design restrictions

The contents of input.txt must be read without a loop, in a single statement.

The program should minimize the number of code lines between the opening and closing of a file.

This is the code I have but im not quite there

Read all of the instructions Instructions Write a Python program that reads

with open("input.txt", "r") as input_file, open("output.txt", "w") as output_file: input_string = input_file.read() input_string = input_string.split (" ") [::1] \#input_string = input_string [::1] output_file.writelines(" ".join(input_string[::-1]))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!