Question

Complete a Python function compare_files(fn1,fn2) to compare file1 and file2, if their content are same return...

Complete a Python function compare_files(fn1,fn2) to compare file1 and file2, if their content are same return True, otherwise return False. should start with def compare_files(f1,f2):

myf3.txt contains:

a
b
c
d

myf2.txt contains:

A
B
C
D

myf1.txt contains:

a
b
c
d

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def compare_files(fn1, fn2):
    try:
        f1 = open(fn1)
        f2 = open(fn2)
        data1 = f1.read()
        data2 = f2.read()
        f1.close()
        f2.close()
        return data1.strip() == data2.strip()
    except FileNotFoundError:
        return False


print(compare_files('myf3.txt', 'myf2.txt'))
print(compare_files('myf3.txt', 'myf1.txt'))

Add a comment
Know the answer?
Add Answer to:
Complete a Python function compare_files(fn1,fn2) to compare file1 and file2, if their content are same return...
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 python Complete the function compare_files(fn1,fn2) so that it takes two file names and if their...

    in python Complete the function compare_files(fn1,fn2) so that it takes two file names and if their content is exactly the same return True, otherwise return False. Note: Capitalization, punctuation and spacing must also be the same. Example: compare_files("myf1.txt", "myf3.txt") returns True

  • Write a simple C/C++ function named int match_content(char * file1, char *file2) that would take two...

    Write a simple C/C++ function named int match_content(char * file1, char *file2) that would take two text file name as a parameter, and return 1, if the content is same in both the files, 0 otherwise. Your program should be case insensitive and should support all the ascii characters.

  • python

    pythonComplete the below function that takes the name of two files, inFilename andoutFilename as arguments and reads the text from the inFilename. In this fileeach line contains the Turkish Republic Identity Number (TCNO), name, surnameand telephone number of a person. Your function should sort all personsaccording to their TCNO, write the sorted data into outFilename. If the fileinFilename does not exist, then the function must create an empty file namedoutFilename.For example, if the function is called such asreadText("in.txt", "out.txt")and in.txt...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

  • In Python please: plagiarism: This Boolean function takes two filenames. If any line occurs in both...

    In Python please: plagiarism: This Boolean function takes two filenames. If any line occurs in both files, return True. If not, return False. I suggest using nested loops. With nested loops, we call the loop that is in the body of the other loop, the "inner loop". The loop that contains the inner loop is the "outer loop". Open the second file inside the outer loop: for line1 in file1: file2 = open(fname2) for line2 in file2:

  • Complete the Python function read_third_line() to read the third line of the file, my2nd.txt. Return the...

    Complete the Python function read_third_line() to read the third line of the file, my2nd.txt. Return the third line as the output of function read_third_line(). Should start with def read_third_line(fname):

  • Use Python [12] 10. Complete the code for the function documented below. Note that the function...

    Use Python [12] 10. Complete the code for the function documented below. Note that the function gets all its input from the parameters and not from the keyboard. It returns all information to the calling function and not to the computer display def frstiggest la, b, c) 'Assume a, b, and c are integers. Return True if a is greater than both b and c, Examples anh False otherwise would be True; firstigges (9, 6, 7) and firstBiagest (4, -2,...

  • in python Complete the function search_file(fname, wd) to take a filename and a word. If the...

    in python Complete the function search_file(fname, wd) to take a filename and a word. If the the word is found in the file contents, the function should return True, otherwise it should return False.

  • python function will Return True if string x contains 3 vowels in a row, in consecutive...

    python function will Return True if string x contains 3 vowels in a row, in consecutive locations, false otherwise. assuming that 'vowels' refer to the following lowercase lttrs: a,e,i,o,u programs fails partially, only allowed to use float, str, int, appen, split, strip, len, range def vowels_three(x): for i in range (o, len(x), 2): if x[i] not in ('a,e,i,o,u'): return False return True

  • Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write...

    Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write a code for function main, that does the following: -creates a variable and assigns it the value True. - uses a while loop which runs as long as the variable of the previous step is True, to get a number from the user and if passing that number to the function of Q3 results in a True value returned, then add that number to...

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