How do I compare files?

C

Clay Hobbs

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
 
G

GHZ

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
 
M

Matimus

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/pil/handbook/index.htm


Matt
 
C

Clay Hobbs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top