Question

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 pair of different lines is found.

Note: Input file will be given by me for testing. You should create your own file and check your program running on that file. You don’t need to attach the txt file. First file name: Master.txt Second file name: Slave.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#------main.py-----
#python3
#method that takes two files and compares them line by line
#and prints the comparision result.
def compareFile(one,two):
try:
#using with open the two files..
with open(one,"r") as master, open(two,"r") as slave:
#read one line from master and slave as well
lineOne = master.readline()
lineTwo = slave.readline()
#till any one of the files are reached to end.
while(lineOne and lineTwo):
#print two lines striped ones, just for printing, while comparing no striping is done
print(lineOne.strip())
print(lineTwo.strip())
#convert the two lines into lower case and compare them for equal
#note: if any one of the files having spaces at the start or at the end
#and the other one dont have spaces considered not same
#if you want to remove spaces and then compare please replace the if condition
#with below if statement.
#if(lineOne.strip().lower() == lineTwo.strip().lower())
if(lineOne.lower() == lineTwo.lower()):
#if same print yes
print("Yes")
else:
#if not print no and break.
print("No")
break
#read next lines from two files.
lineOne = master.readline()
lineTwo = slave.readline()
#after the end of while loop there may be a condition that
#one of the files are read completely and next are not
#so at that situation
#on successfull completion of reading entire file variables have eof or null
#using that value if the lineOne is not having null and lineTwo is having null(not null is True)
#or the vice versa
#then that situation is files are not same.
if((lineOne and not lineTwo) or (not lineOne and lineTwo)):
#so print lines and print no;
#if you want to remove print statements please comment them.
print(lineOne)
print(lineTwo)
print("No")
#if any exception got while reading or opening print the message
except Exception as e:
print("Error occured while processing: ",e)
#function that takes user input for two files. and returns them.
def getFiles():
one = input("Enter first File Name: ").strip()
two = input("Enter second File name: ").strip()
return one,two

#get user input for files.
one,two = getFiles()
#call compareFile(one,two) by passing user enter files.
compareFile(one,two)


#--------------- Master.txt ----------------

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 pair of different lines is found.

#------------------ Slave.txt ------------------

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 pair of different lines is found.

#if you have any doubts please comment and please like the answer.

Add a comment
Know the answer?
Add Answer to:
Write a python program that prompts the user for the names of two text files 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
  • For Python | Instructions Write a script named difpy. This script should prompt the user for...

    For Python | Instructions Write a script named difpy. This script should prompt the user for the names of two text files and compare the contents of the two files to see if they are the same. 1. If they are the script should simply output "Yes". 2. If they are not the script 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...

  • computer programming python

    Write a script named dif.py. This script should prompt 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 script should simply output "Yes". If they are not, the script should output "No", followed by the first lines of each file that differ from eachother. The input loop should read and compare lines from each file. The loop should break as soon as...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • PYTHON 3.6.1 write a script named copyfile.py. this script should prompt the user for the names...

    PYTHON 3.6.1 write a script named copyfile.py. this script should prompt the user for the names of two text files. the contents of the first file should be input and written to the second file.

  • *12.1 (Name for both genders) Write a program that prompts the user to enter one of...

    *12.1 (Name for both genders) Write a program that prompts the user to enter one of the filenames described in Exercise 12.31 and displays the names that are used for both genders in the file. Here is a sample run: Enter a file name for baby name ranking: Babynamesranking2001.txt 69 names used for both genders They are Tyler Ryan Christian ... The original question had me enter a URL and file to access all the data, but the website no...

  • Write a program that prompts the user to enter a list of names. Each person's name...

    Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space (: and the names are entered lastName, firstName (i.e. separated by ',). Your program should then print out the names, one per line, with the first names first followed by the last names. A sample run of your program should look like Please enter your list of names: Epstein, Susan; St. John, Katherine;...

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