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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started