In Python, is there a concise way of comparing whether the contents of two text files are the same?

后端 未结 8 616
我在风中等你
我在风中等你 2020-11-29 01:51

I don\'t care what the differences are. I just want to know whether the contents are different.

8条回答
  •  攒了一身酷
    2020-11-29 02:31

    Since I can't comment on the answers of others I'll write my own.

    If you use md5 you definitely must not just md5.update(f.read()) since you'll use too much memory.

    def get_file_md5(f, chunk_size=8192):
        h = hashlib.md5()
        while True:
            chunk = f.read(chunk_size)
            if not chunk:
                break
            h.update(chunk)
        return h.hexdigest()
    

提交回复
热议问题