How do I compare files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clay Hobbs

    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

  • GHZ

    #2
    Re: How do I compare files?

    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.r ead())
    digest = m.hexdigest()
    file.close()


    and compare the digest on both files

    Comment

    • Matimus

      #3
      Re: How do I compare files?

      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").r ead()
      data2 = urllib.urlopen( "http://url.of.jpg2").r ead()

      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").r ead()
      data2 = urllib.urlopen( "http://url.of.jpg2").r ead()

      im1 = Image.fromstrin g(data1)
      im2 = Image.fromstrin g(data2)
      result = ImageChops.diff erence(im1, im2)
      result.save("re sult_image.jpg" )


      Read up on PIL:




      Matt

      Comment

      • Larry Bates

        #4
        Re: How do I compare files?

        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(fi lename1).read() ).hexdigest()
        md5file2 = md5.md5(open(fi lename2).read() ).hexdigest()

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

        -Larry

        Comment

        • Clay Hobbs

          #5
          Re: How do I compare files?


          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").r ead()
          data2 = urllib.urlopen( "http://url.of.jpg2").r ead()
          >
          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

          Comment

          Working...