I want use MD5 to compare both folder in Python.

Following steps that i want to compare both folder by using MD5:

1. After all the files downloaded to folderA.
2. All the files inside folderA will copy to folderB.
3. Then MD5 will compare both folder is correct hash.
4. Then return True.

Dani AI

Generated

Building on 's steps and the suggestions from and : compute a digest for each file in folderA and folderB (use the same relative paths) and compare those mappings. Use Python's high-level hashing API and a directory walker so the solution handles nested folders and large files without loading whole files into memory. See the standard hashlib and os.walk docs for the API and behavior. (docs.python.org)

Here is a compact, practical pattern to use (open files in binary mode and read in chunks):

import os
import hashlib

def file_md5(path, chunk_size=8192):
    h = hashlib.md5()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(chunk_size), b""):
            h.update(chunk)
    return h.hexdigest()

def dir_checksums(root):
    root = os.path.abspath(root)
    checksums = {}
    for dirpath, dirnames, filenames in os.walk(root):
        for name in filenames:
            full = os.path.join(dirpath, name)
            rel = os.path.relpath(full, root)
            checksums[rel] = file_md5(full)
    return checksums

def compare_dirs(a, b):
    return dir_checksums(a) == dir_checksums(b)

Notes and cautions: open files with "rb" (binary) to avoid text-mode differences. Be careful with symlinks (os.walk defaults to not following them) and with differing file metadata — this method compares content only. MD5 is fine for accidental corruption or quick integrity checks, but it is not collision-resistant for security-critical checks; prefer SHA-256 for tamper-detection. For directory-level utilities, Python also offers filecmp (dircmp) for higher-level comparisons. (rfc-editor.org)

Relevant docs:

Recommended Answers

All 4 Replies

Sooo. What work have you done? Its nice to see a base from which we can help you.

Generally module md5 is used to encrypt strings/texts like passwords by making a unique check sum of the string. Once encrypted you cannot decrypt the resulting check sum back to the original string. The only way you can find out its contents is to encrypt another string and compare the md5 check sums.

Here is an example ...

# Note that Python30 dropped md5 and uses hashlib.md5()

import md5
mystring = "meet me at Karl's bar on Friday the tenth at 9PM"
print( md5.md5(mystring).hexdigest() )

You can save the text to a file and attach the hexdigest string as a final line.

You're probably going to want to use some recursion when you're searching through the directories. If you copy subfolders you're going to want to know if the files in the subfolders have also copied properly.

You can see some examples of using recursion to list all the files in a directory and its subdirectories in this thread: http://www.daniweb.com/forums/thread234497.html

Here is an example ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.