473,407 Members | 2,676 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,407 software developers and data experts.

How do I compare files?

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?

-- Ratfink

Jul 22 '08 #1
4 4019
GHZ
On Jul 23, 1:27*am, Clay Hobbs <c...@lakeserv.netwrote:
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?

-- Ratfink
import hashlib

file = open(path)
m = hashlib.md5()
m.update(file.read())
digest = m.hexdigest()
file.close()
and compare the digest on both files
Jul 23 '08 #2
On Jul 22, 4:27*pm, Clay Hobbs <c...@lakeserv.netwrote:
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?

-- Ratfink
Do you just want to check to see if they are the same? Or do you
actually want to know what the differences are?

import urllib

data1 = urllib.urlopen("http://url.of.jpg1").read()
data2 = urllib.urlopen("http://url.of.jpg2").read()

if data1 == data2:
print "they are the same"
Doing a regular text diff won't tell you much. You could use PIL and
do all sorts of image manipulation though. You might generate an image
of the difference between each pixel.
Something like this:

import Image
import ImageChops
import urllib

data1 = urllib.urlopen("http://url.of.jpg1").read()
data2 = urllib.urlopen("http://url.of.jpg2").read()

im1 = Image.fromstring(data1)
im2 = Image.fromstring(data2)
result = ImageChops.difference(im1, im2)
result.save("result_image.jpg")
Read up on PIL:

http://www.pythonware.com/library/pi...book/index.htm
Matt

Jul 23 '08 #3
Clay Hobbs wrote:
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?

-- Ratfink
Use md5 to calculate checksum:

import md5

md5file1 = md5.md5(open(filename1).read()).hexdigest()
md5file2 = md5.md5(open(filename2).read()).hexdigest()

if md5file1 != mdfile2:
#
# Do whatever you want
#

-Larry
Jul 23 '08 #4

On Tue, 2008-07-22 at 17:29 -0700, Matimus wrote:
On Jul 22, 4:27pm, Clay Hobbs <c...@lakeserv.netwrote:
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?

-- Ratfink

Do you just want to check to see if they are the same? Or do you
actually want to know what the differences are?

import urllib

data1 = urllib.urlopen("http://url.of.jpg1").read()
data2 = urllib.urlopen("http://url.of.jpg2").read()

if data1 == data2:
print "they are the same"
I just wanted to see if they are different. The code I was using that
didn't work was almost the same, except that the lines that download the
files didn't end in .read(). Thank you for your help

-- Ratfink

Jul 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nicolas Fleury | last post by:
Hi everyone, Is there a way to compare recursively two objects (compare their members recursively)? I'm only interested in equality or non-equality (no need for lower-than...). Thx and...
1
by: Frank | last post by:
Hi, is there a way to use XSL to compare two XML files to verify if a "record" in an XML file has changed of parent in another XML file ? I am trying to implement a template in an XSL stylesheet...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
2
by: SP | last post by:
Hi All, I need to compare two files line by line and copy the differing lines to a new files. I.e. Compare file1 and file2 line by line. Copy only differing lines to file3. I tried a couple...
5
by: drabee | last post by:
Please help 2 things: 1-I need c# code to compare 2 audio files .or any other .net code 2-code to receive bluetooth file from mobile and save it using .net code
0
by: bobbymusic | last post by:
In my form I have two rich text boxes.In both of them I put two different text files(.txt or .rtf).I looking for some code to compare them, and to have the result as marked differences into the...
6
by: yinglcs | last post by:
Hi, i have 2 files which are different (1 line difference): $ diff groupresult20070226190027.xml groupresult20070226190027-2.xml 5c5 < x:22 y:516 w:740 h:120 area: --- But when I use the...
8
by: Perl Beginner | last post by:
I am new to Perl and new to this site. I have the same question that I keep seeing, but not finding an answer…why doesn’t the compare function work? I’ve been going at this for a while. My code is...
1
by: =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= | last post by:
II want to compare how many seconds there are between files. If the files are within a 1 - 10 second range I want to copy them to their own folders. What I have so far is a couple methods that take...
12
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.