By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

How to compare two txt files to see if they are the same?

P: 23
I am trying to write a program that compares two txt file to see if they are the same. The program is to promt the user for two txt files and then compare them. If they are the same it should print yes but if not it should print no followed by the first line of each file that is diffeent. I know how to promt the user for the filename but i do not know how to compare them.
Oct 28 '10 #1
Share this Question
Share on Google+
12 Replies

Expert Mod 2.5K+
P: 2,851
For small files, both could be read into memory as strings and compared for equality as in:
Expand|Select|Wrap|Line Numbers
  1. >>> s1 = "abcdef"
  2. >>> s2 = "abcdef"
  3. >>> s1 == s2
  4. True
  5. >>> s3 = "xyz"
  6. >>> s1 == s3
  7. False
  8. >>> 
Alternatively, you can use module filecmp to compare files.
Oct 28 '10 #2

P: 23
i have import them in and then comare them! How do i do that?
Oct 29 '10 #3

Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. s1 = open(file_name1).read()
  2. s2 = open(file_name2).read()
  3. if s1 == s2:
  4.     print "File contents are identical"
  5. else:
  6.     print "File contents are different"
Oct 29 '10 #4

P: 23
I actually got that far on my own but thanks anyways. Can you help me figure out how to make it print the first line in each file that is different?
Oct 30 '10 #5

P: 23
to open the file is what you have the same as:
infile = open ("filename" , "r")
Oct 30 '10 #6

P: 23
This is what i have, but it won't run right.
Expand|Select|Wrap|Line Numbers
  1. file1 = raw_input("What is the name of the first sile including the .txt?")
  2. file2 = raw_input("what is the name of the second file including the .txt?")
  4. file1 = open("file1" , "r")
  5. file2 = open("file2", "r")
  7. if file1 == file2:
  8.     print("Yes")
  9. else:
  10.     print("No")
Oct 30 '10 #7

Expert 100+
P: 624
For opening and reading files with a for() loop, read section 9.1 here.
Oct 30 '10 #8

P: 23
The way i have it it says no such file or directory on line 4. How do i fix it?
Oct 31 '10 #9

Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. fn1 = raw_input("Enter file name.")
  2. fileobj1 = open(fn1, 'r')
The file name is a text string and is assigned to the identifier fn1. An open file object is returned by built-in function open() and assigned to identifier fileobj1. Notice the identifier fn1 is the argument representing the file name.
Oct 31 '10 #10

P: 23
I tried this and it still says no such file or directory
Oct 31 '10 #11

P: 23
Here is what i have:

Expand|Select|Wrap|Line Numbers
  1. fn1 = raw_input("What is the name of the first file (be sure to include .txt after the filename)?")
  2. fn2 = raw_input("What is the name of the second file (be sure to include .txt after the filename)?")
  3. fileobj1 = open("fn1", "r")
  4. fileobj2 = open("fn2", "r")
  5. if file1 == file2:
  6.     print ("Yes these files are the same.")
  7. else:
  8.     print("No these file are not the same.")
Oct 31 '10 #12

Expert 100+
P: 624
You have to supply the complete path to the file, unless it is in PYTHONPATH, see here.
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.path
  3. ##
  4. ## if the path to the file is not in the above then you have to provide the path
  5. fileobj1 = open("/path/to/fn1", "r")
  6. ##
  7. ## or add it to sys.path
  8. sys.path.append("/path/to")
  9. fileobj1 = open("fn1", "r") 
Oct 31 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.