15

I have a python app in which I am creating packages in windows to be used and later compared in a linux python app. I am creating an md5 for a file in windows to be checked later in linux. The problem is that the same code on the same file gives different Md5 hash results in each environment. Below is the method I use to calculate the Md5. (It is the same code on each end, and I am using Python 2.6.5 for both windows/linux environments) When I run this on the same file in different environments, I get md5 hashes that do not match.

def md5_for_file(filePath):
        md5 = hashlib.md5()
        file = open(filePath)
        while True:
            data = file.read(8192)
            if not data:
                break
            md5.update(data)

        file.close()   
        return md5.hexdigest()

Any ideas or suggestions are appreciated.

2 Answers 2

26

Change open(filePath) to open(filePath, 'rb'), where the b is for binary mode. You're currently opening in text mode, which can cause portability issues.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! I now get matching MD5's on both systems. Thank you
0

check if the two files use the same encoding and lineendings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.