计算列表中文件的 MD5 校验和

发布于 2024-10-19 14:32:28 字数 1179 浏览 3 评论 0原文

我正在用 Python 做一个项目。我有一个文件列表,我想获取其 md5 校验和。然后它将校验和存储在另一个列表中。然后它再次检查并检查它们是否不同。我有让校验和工作的功能,但现在我不知道如何将它们添加到列表中。这就是我正在尝试的


import sys, hashlib
files = ['/home/file1', '/home/file2', '/home/file3', '/etc/passwd']
md5s = []
def getmd5(file, ex="", inc=""):
    m = hashlib.md5()
    try:
        fd = open(file,"rb")
    except IOError:
        print "Can't retrieve MD5sum for ", file
        pass
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        if ex and eachLine.startswith(ex):
            continue
        m.update(eachLine)
    m.update(inc)
    a = m.hexdigest()
    md5s.append(a)

对于列表中的 i: 得到md5(i) 打印md5

But when I try this I get 4 lists like so:

['729aebf5b3a841d3ef815e297ae2ce07']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad', '0c01d98119386db13beb1bfdbae7ba2b']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad', '0c01d98119386db13beb1bfdbae7ba2b', 'b51c93a2f965b75de903d159720dd6e6']

我想做的是将每个哈希校验和存储在 md5s 列表中,然后读取它们是否不同。

I'm working on a bit of a project in Python. I have a list of files that I want to get the md5checksums for. Then it stores the checksums in another list. Then it checks again and checks to see if they're different. I have the function for getting the checksums working but now I can't figure out how I'd add them to a list. Here is what I'm trying


import sys, hashlib
files = ['/home/file1', '/home/file2', '/home/file3', '/etc/passwd']
md5s = []
def getmd5(file, ex="", inc=""):
    m = hashlib.md5()
    try:
        fd = open(file,"rb")
    except IOError:
        print "Can't retrieve MD5sum for ", file
        pass
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        if ex and eachLine.startswith(ex):
            continue
        m.update(eachLine)
    m.update(inc)
    a = m.hexdigest()
    md5s.append(a)

for i in lists: getmd5(i) print md5s


But when I try this I get 4 lists like so:


['729aebf5b3a841d3ef815e297ae2ce07']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad', '0c01d98119386db13beb1bfdbae7ba2b']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad', '0c01d98119386db13beb1bfdbae7ba2b', 'b51c93a2f965b75de903d159720dd6e6']

What I'd like to do is store each hash checksum in the md5s list and then read to see if they're different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-10-26 14:32:28
def getmd5(filename, ....):

  return m.hexdigest()


md5s = dict()

for fname in filenames:
   md5s[fname] = getmd5(fname)

print md5s
def getmd5(filename, ....):

  return m.hexdigest()


md5s = dict()

for fname in filenames:
   md5s[fname] = getmd5(fname)

print md5s
稀香 2024-10-26 14:32:28

你没有 4 个列表。每次调用 getmd5 后都会打印 md5s 的内容,这会将一个 md5 哈希值添加到列表中。您恰好执行了 4 次此操作,因为您的文件列表中有 4 个项目。

这意味着您有一个列表,它包含最后一个 for 循环末尾的所有摘要。您正在构建列表的 for 循环内进行打印,因此您无法理解它。删除打印前的缩进以查看所需格式的结果。

You don't have 4 lists. You're printing the contents of the md5s each time after you call getmd5 which adds one md5 hash to the list. You just happen to be doing this 4 times, because you have 4 items in your files list.

This means you have one list and it contains all the digests at the end of last for loop. You are printing inside the for loop constructing the list and hence you are not able to understand it. Remove the indent before the print to see the result in the format that you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文