Question

IN PYTHON Write a program that reads two text files, take one sentence from each text...

IN PYTHON

Write a program that reads two text files, take one sentence from each text file and print them one after another. Example: If line1 is from text1.txt and line2 is from text2.txt, then print line1 line2 Repeat this for all the lines of the two files

0 0
Add a comment Improve this question Transcribed image text
Answer #1
try:
    f1 = open("text1.txt")
    f2 = open("text2.txt")

    lines1 = f1.readlines()
    lines2 = f2.readlines()

    i = 0
    while i < len(lines1) or i < len(lines2):
        if i < len(lines1):
            print(lines1[i].strip())
        if i < len(lines2):
            print(lines2[i].strip())
        i += 1

    f1.close()
    f2.close()
except FileNotFoundError:
    print("Input files does not exists")

Add a comment
Know the answer?
Add Answer to:
IN PYTHON Write a program that reads two text files, take one sentence from each text...
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
  • C++.Write a program that opens two text files and reads their contents into two separate queues....

    C++.Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. Sample Run Suppose we have two files...

  • In Python, do a basic encryption of a text file in the following manner. The program...

    In Python, do a basic encryption of a text file in the following manner. The program encrypt.py will read in the following text file and rearrange the lines in the file randomly and save the rearranged lines of txt to another file called encrypted.txt. It will also save another file called key.txt that will contain the index of the lines that were rearranged in the encrypted file, so for example if the 4th line from the original file is now...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • python Write a program that reads the name of fruits and the colors from an input...

    python Write a program that reads the name of fruits and the colors from an input file fruit. txt. Each fruit name and the color is given on a line as shown below separated by space Apple Red Orange Orange Grape Green Now write output to an output file (fruit2.txt) as shown below Color of Apple is Red Color of Orange is Orange Color of Grape is Green The program has to be general to read in as many lines...

  • Python Write the list of all the files in the directory ptest_0 , in a text...

    Python Write the list of all the files in the directory ptest_0 , in a text file contents.txt (this file already exists there.) in separate lines. 1) import glob import shutil import os Beginning_folder = r"/home/MikeyV/Laptop/test_0/learn0_0"; Last_folder = "/home/MikeyV/Laptop/ptest_0"; try:    os.makedirs(last_folder); except;    print"Folder already exists..."; for txt _file in glob.glob(beginning_folder+"\\*txt"):    Shuril.copy2(txt_file,last_folder);

  • Write an assembly language 32 bit program that reads in lines of text by a .txt...

    Write an assembly language 32 bit program that reads in lines of text by a .txt file and read in from the user and prints them diagonally. Using good commenting.

  • 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...

  • Write a Python program that reads text from a file, encrypts it with a Caesar Cipher,...

    Write a Python program that reads text from a file, encrypts it with a Caesar Cipher, and displays the encrypted text. Do not process punctuation. Convert the original string to all lower-case before encrypting it.

  • the language is python Instructions Forum Tutoring Problem program. Write a function concatenate_files(filename1, filename2, new filename) that concatenates the text from two source files such...

    the language is python Instructions Forum Tutoring Problem program. Write a function concatenate_files(filename1, filename2, new filename) that concatenates the text from two source files such that the text from the file named by argument filename 2 follows the text from filename1. The concatenated text is written to a new file with the name given by new_filename. Your function O must not return anything. We have provided sample input files named part1.txt and part2.txt containing a portion of the text from...

  • Write a program to count a number of files for each extension in the given directory....

    Write a program to count a number of files for each extension in the given directory. The program should take a directory name as argument and print count and extension for each available file extension. For example, the output would be something like this: python counter.py \yourpathhere 14 py 4 txt 1 csv

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