Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 264889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:43:23+00:00 2026-05-11T22:43:23+00:00

Possible Duplicates: Finding duplicate files and removing them. In Python, is there a concise

  • 0

Possible Duplicates:
Finding duplicate files and removing them.
In Python, is there a concise way of comparing whether the contents of two text files are the same?

What is the easiest way to see if two files are the same content-wise in Python.

One thing I can do is md5 each file and compare. Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:43:24+00:00Added an answer on May 11, 2026 at 10:43 pm

    Yes, I think hashing the file would be the best way if you have to compare several files and store hashes for later comparison. As hash can clash, a byte-by-byte comparison may be done depending on the use case.

    Generally byte-by-byte comparison would be sufficient and efficient, which filecmp module already does + other things too.

    See http://docs.python.org/library/filecmp.html
    e.g.

    >>> import filecmp
    >>> filecmp.cmp('file1.txt', 'file1.txt')
    True
    >>> filecmp.cmp('file1.txt', 'file2.txt')
    False
    

    Note that by default, filecmp does not compare the contents of the files, to do so, add a third parameter shallow=False.

    Speed consideration:
    Usually if only two files have to be compared, hashing them and comparing them would be slower instead of simple byte-by-byte comparison if done efficiently. e.g. code below tries to time hash vs byte-by-byte

    Disclaimer: this is not the best way of timing or comparing two algo. and there is need for improvements but it does give rough idea. If you think it should be improved do tell me I will change it.

    import random
    import string
    import hashlib
    import time
    
    def getRandText(N):
        return  "".join([random.choice(string.printable) for i in xrange(N)])
    
    N=1000000
    randText1 = getRandText(N)
    randText2 = getRandText(N)
    
    def cmpHash(text1, text2):
        hash1 = hashlib.md5()
        hash1.update(text1)
        hash1 = hash1.hexdigest()
        
        hash2 = hashlib.md5()
        hash2.update(text2)
        hash2 = hash2.hexdigest()
        
        return  hash1 == hash2
    
    def cmpByteByByte(text1, text2):
        return text1 == text2
    
    for cmpFunc in (cmpHash, cmpByteByByte):
        st = time.time()
        for i in range(10):
            cmpFunc(randText1, randText2)
        print cmpFunc.func_name,time.time()-st
    

    and the output is

    cmpHash 0.234999895096
    cmpByteByByte 0.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 185k
  • Answers 185k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "Files" inside .jar files are not files to the operating… May 12, 2026 at 5:06 pm
  • Editorial Team
    Editorial Team added an answer What if you install say, phpbb and bridge it with… May 12, 2026 at 5:06 pm
  • Editorial Team
    Editorial Team added an answer Well, instead of passing Application.Current.MainWindow, just pass a reference to… May 12, 2026 at 5:06 pm

Related Questions

Possible Duplicate: How many Python classes should I put in one file? Coming from
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: Make Visual Studio understand CamelCase when hitting ctrl and cursor keys Exact
Possible Duplicates: Visual Studio 6 tips and tricks Visual Studio 2005 Shortcuts Favorite Visual

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.