Question

Write a program in python that reads each line in a file, reverses its lines, and...

Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had a little lamb
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code:

Input file(read.txt)

Output file(newfile.txt)

Code to copy:

#Open the input file in read mode.
myfile=open("text.txt",'r')

#Create the output file, newtext.txt and open it in write mode.
myfile1=open("newtext.txt","w")

#Read the contents of the input file and store it in variable s
s=myfile.readlines()

#Write the reversed contents in the output file
myfile1.writelines(reversed(s))

#Close both the files.
myfile.close()
myfile1.close()

Add a comment
Know the answer?
Add Answer to:
Write a program in python that reads each line in a file, reverses its lines, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Using python Question 13 (20 points) Write a function named Linestats that finds the number of...

    Using python Question 13 (20 points) Write a function named Linestats that finds the number of consonant and vowel letters on each line of a file and writes those statistics to a corresponding line of a new file, separated by a space. Definitions: A vowel letter is one of a, e, i, o, u, along with the corresponding uppercase letters..A consonant letter is not a vowel letter. The function linestats takes two parameters: 1. inFile, a string, the name of...

  • python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form &#...

    python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form 'SPAM-Confidence: float number'. When you encounter a line that starts with "SPAM-Confidence:" extract the floating-point number on the line. The function returns the count of the lines where the confidence value is greater than 0.8. For example, if 'spam.txt' contains the following lines along with normal text getSpamLines('spam.txt') returns 2. SPAM-Confidence: 0.8945 Mary had a little lamb. SPAM-Confidence: 0.8275 SPAM-Confidence: 0.7507 The...

  • How to write a Java program that reads the file "input.txt" and writes all even values...

    How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • c++ Write a program that reads a file, removes any blank lines at the beginning or...

    c++ Write a program that reads a file, removes any blank lines at the beginning or end of the file, and writes the remaining lines back to the same file.

  • (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

    (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons (;) or pound signs (#) should be ignored. All empty lines must be ignored Lines containing a string between square brackets are sections. Within each section, lines may contain numbers or strings. For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that. Example input: ; this is a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT