473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
12 2833
bvdet
2,851 Expert Mod 2GB
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
i have import them in and then comare them! How do i do that?
Oct 29 '10 #3
bvdet
2,851 Expert Mod 2GB
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
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
to open the file is what you have the same as:
infile = open ("filename" , "r")
Oct 30 '10 #6
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?")
  3.  
  4. file1 = open("file1" , "r")
  5. file2 = open("file2", "r")
  6.  
  7. if file1 == file2:
  8.     print("Yes")
  9. else:
  10.     print("No")
  11.  
Oct 30 '10 #7
dwblas
626 Expert 512MB
For opening and reading files with a for() loop, read section 9.1 here.
Oct 30 '10 #8
The way i have it it says no such file or directory on line 4. How do i fix it?
Oct 31 '10 #9
bvdet
2,851 Expert Mod 2GB
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
I tried this and it still says no such file or directory
Oct 31 '10 #11
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.")
  9.  
Oct 31 '10 #12
dwblas
626 Expert 512MB
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

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

Similar topics

4
by: Lad | last post by:
Hi, What is the best method for comparing two files by words? I was thinking about reading files by words and compare them but a word in one file can be linked with a new line character ( \n) and...
0
by: Grahammer | last post by:
Just wondering if I can get the Visual Studio .Net IDE editor to treat my ..INC files (included files) the same way as .ASP files? The .INC files are just chunks of ASP code anyhow. Thanks!
8
by: pjsimon | last post by:
I want to compare two files like MS Visual SourceSafe's Show Differences feature. Is there a way to access methods in VB.Net that will let me use existing MS code to show the differences between...
0
by: Michael Buechel | last post by:
I have a solution with 5 projects inside. Some of the projects have same named files with same named classes inside. VSS 2005 gets confused by double clicking on the class or function in the class...
4
by: Dipanwita | last post by:
I have been working in VC++ 6.0. There are two header files with same name.The path of old version is included via "TOOLS/option/directories" I want to use new version though don't want to change...
3
by: shona | last post by:
Hi, can any one told me how to compare files with same name but different extension.. for eg. if a.txt & a.doc then ans is same files.. Thanks
0
by: ds81 | last post by:
I am trying to read a large number of image (BMP, JPG) files, and need to know if any are identical. I have been trying to store the hashcodes of the files, so that they then can be compared later. ...
4
by: Clay Hobbs | last post by:
I am making a program that (with urllib) that downloads two jpeg files and, if they are different, displays the new one. I need to find a way to compare two files in Python. How is this done? ...
0
by: norseman | last post by:
Timothy Grant wrote: =================================== If you are on a Unix platform: man cmp man identify man display (ImageMagick) gimp If you use mc (MidnightCommander) the F3 key can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.