2

This is my code so far I'm trying to compare the files given by the user and printing if the content inside both files is the same. If it is the same string content I would like to print Yes if it's not, print No along with the words inside both files.

print ('Enter the first file name: ', end = '')
FIRST_FILE = input()

print ('Enter the second file name: ', end = '')
SECOND_FILE = input()

if SECOND_FILE == line in FIRST_FILE:
    print('Yes')
else:
    print('No')

infile = open('one.txt', 'w')
infile.write('Hello') 
infile.close() 

infile2 = open('SameAsone.txt', 'w')
infile2.write('Hello') 
infile2.close()

infile3 = open('DifferentFromone.txt', 'w')
infile3.write('Bye') 
infile3.close() 

Thanks.

Nanor
  • 79
  • 2
  • 7
  • 2
    Are you comparing the string content of the files (text-by-text), the "ID" of the files, or the byte content of the files? –  Feb 23 '19 at 05:39
  • 1
    The string content. Sorry for not clarifying. – Nanor Feb 23 '19 at 05:40
  • `SECOND_FILE` and `FIRST_FILE` is a string, why you write `line in SECOND_FILE`? And what's intent for next three blocks to writing a file? – pwxcoo Feb 23 '19 at 05:43
  • That's is just me trying things out. I'm not sure exactly how to do it – Nanor Feb 23 '19 at 05:46
  • This question may be a duplicate of https://stackoverflow.com/questions/21747112/how-to-compare-two-files-in-python –  Feb 23 '19 at 05:53

4 Answers4

3

A simple approach would be to use filecmp

import filecmp
check = filecmp.cmp('file1.txt', 'file1.txt')
print ('No', 'Yes')[check]

If you want more information see docs

skaul05
  • 2,154
  • 3
  • 16
  • 26
  • 1
    I love this answer! Thanks for reminding me of `filecmp` also you could do: `print ('No', 'Yes')[check]` in place of the `if: else:` just to put it out there – Jab Feb 23 '19 at 05:51
  • Edited! Thanks for the insight @Jab – skaul05 Feb 23 '19 at 06:00
  • Thanks this helped a bunch. Now how would I print the strings in both files under 'No'? – Nanor Feb 23 '19 at 06:16
  • @skaul05 No problem, gotta love Boolean indexing – Jab Feb 23 '19 at 06:16
  • @Nanor take a look at [difflib](https://docs.python.org/3.7/library/difflib.html#module-difflib) in specific `Differ.compare`. Also that may be a separate question after some research. – Jab Feb 23 '19 at 06:20
  • Seems like this does not work if the `file1.txt` file is changed during the runtime – alper Oct 01 '21 at 11:58
1

You can use .read also I suggest using the with statement as there would be no need to close the files manually.

def compare_files(fn1, fn2):
    with open(fn1, 'r') as file1, open(fn2, 'r') as file2:
        return file1.read() == file2.read()


first_file = input('Enter the first file name: ')
second_file = input('Enter the second file name: ')

print(['No', 'Yes'][compare_files(first_file, second_file)])
S.B
  • 13,077
  • 10
  • 22
  • 49
Jab
  • 26,853
  • 21
  • 75
  • 114
0

You can achieve this more efficiently using use collections module Counter method and OrderedDict preserve the order of the line.

from collections import Counter, OrderedDict

with open("one.txt") as file_one, open("two.txt") as file_two:
    if OrderedDict(Counter(file_one)) == OrderedDict(Counter(file_two)):
        print("matched")
    else:
        print("not macthed")
Hari
  • 1,545
  • 1
  • 22
  • 45
0

A simple approach is to read both files using f.read() where f is the file being opened in read ('r') mode. The read() operation returns the string content of the files.

We then compare the read content of the files using == to determine if the sequence of strings are identical.

Letting fileA, fileB be existent filenames, Hence, the minimal file-comparison code should be:

  f = open(fileA, 'r')
  contentA = f.read()
  f.close()
  f = open(fileB, 'r')
  contentB = f.read()
  f.close()

  result = "No"
  if contentA == contentB:
    result = "Yes"

You should also handle cases where one of the files are not present (the minimal code returns a traceback if any of fileA, fileB refers to a nonexistent file.