相关疑难解决方法(0)

在Python中获取大文件的MD5哈希值

我使用了hashlib(它取代了Python 2.6/3.0中的md5),如果我打开一个文件并将其内容放入hashlib.md5()函数中,它工作正常.

问题在于非常大的文件,它们的大小可能超过RAM大小.

如何在不将整个文件加载到内存的情况下获取文件的MD5哈希值?

python md5 hashlib

183
推荐指数
7
解决办法
11万
查看次数

如何在Python中计算文件的md5校验和?

我在python中创建了一个代码,用于检查文件中的md5并确保md5与原始文件匹配.这是我开发的:

#Defines filename
filename = "file.exe"

#Gets MD5 from file 
def getmd5(filename):
    return m.hexdigest()

md5 = dict()

for fname in filename:
    md5[fname] = getmd5(fname)

#If statement for alerting the user whether the checksum passed or failed

if md5 == '>md5 will go here<': 
    print("MD5 Checksum passed. You may now close this window")
    input ("press enter")
else:
    print("MD5 Checksum failed. Incorrect MD5 in file 'filename'. Please download a    new copy")
    input("press enter") 
exit
Run Code Online (Sandbox Code Playgroud)

但每当我运行代码时,我得到以下内容:

Traceback (most recent call last):
File "C:\Users\Username\md5check.py", …
Run Code Online (Sandbox Code Playgroud)

python md5 md5sum python-3.x

57
推荐指数
3
解决办法
12万
查看次数

使用hashlib在Python 3中计算文件的md5摘要

使用python 2.7,以下代码计算文件内容的mD5 hexdigest.

(编辑:嗯,不是因为答案已经显示,我只是这么认为).

import hashlib

def md5sum(filename):
    f = open(filename, mode='rb')
    d = hashlib.md5()
    for buf in f.read(128):
        d.update(buf)
    return d.hexdigest()
Run Code Online (Sandbox Code Playgroud)

现在,如果我使用python3运行该代码,则会引发TypeError异常:

    d.update(buf)
TypeError: object supporting the buffer API required
Run Code Online (Sandbox Code Playgroud)

我发现我可以使用python2和python3运行代码,将其更改为:

def md5sum(filename):
    f = open(filename, mode='r')
    d = hashlib.md5()
    for buf in f.read(128):
        d.update(buf.encode())
    return d.hexdigest()
Run Code Online (Sandbox Code Playgroud)

现在我仍然想知道为什么原始代码停止工作.看来,当使用二进制模式修饰符打开文件时,它返回整数而不是编码为字节的字符串(我说因为type(buf)返回int).这种行为是在某处解释的吗?

python hashlib python-3.x

22
推荐指数
2
解决办法
2万
查看次数

Python 中的 Google Cloud Storage 客户端是否会自动检查 CRC(或 MD5)?

我试图找出GCS Python 客户端,更具体地说,blob.upload_from_file()并自动blob.download_to_file()检查上传或下载文件的完整性。如果没有,我如何以编程方式检查 CRC 哈希?任何指向文档或源代码的指针将不胜感激。

python integrity google-cloud-storage google-cloud-platform

5
推荐指数
1
解决办法
2388
查看次数

如何获取文件夹中的所有文件并在python中获取MD5哈希?

我正在尝试编写一些代码来获取文件夹中每个exe文件的md5.

我的问题是我不明白该怎么做.仅当文件夹仅包含一个文件时,它才有效.这是我的代码:

import glob
import hashlib
file = glob.glob("/root/PycharmProjects/untitled1/*.exe")

newf = str (file)
newf2 =  newf.strip( '[]' )
newf3 = newf2.strip("''")

with open(newf3,'rb') as getmd5:
    data = getmd5.read()
    gethash= hashlib.md5(data).hexdigest()
    print gethash
Run Code Online (Sandbox Code Playgroud)

我得到了结果:

a7f4518aae539254061e45424981e97c
Run Code Online (Sandbox Code Playgroud)

我想知道如何对文件夹中的多个文件执行此操作.

python md5

3
推荐指数
1
解决办法
5614
查看次数

md5sum shell 脚本和 python hashlib.md5 不同

我正在比较两个不同位置的两个 qcow2 图像文件以查看差异。/opt/images/file.qcow2 /mnt/images/file.qcow2

当我跑

md5sum /opt/images/file.qcow2 
md5sum  /mnt/images/file.qcow2
Run Code Online (Sandbox Code Playgroud)

两个文件的校验和相同

但是当尝试使用以下代码找到 md5sum 时

def isImageLatest(file1,file2):
    print('Checking md5sum of {} {}'.format(file1, file2))

    if os.path.isfile(file1) and os.path.isfile(file2):
        md5File1 = hashlib.md5(file1).hexdigest()
        md5File2 = hashlib.md5(file2).hexdigest()
        print('md5sum of {} is {}'.format(file1, md5File1))
        print('md5sum of {} is {}'.format(file2, md5File2))
    else:
        print('Either {} or {} File not found'.format(file1,file2))
        return False

    if md5File1 == md5File2:
        return True
    else:
        return False
Run Code Online (Sandbox Code Playgroud)

它说校验和不一样

更新 文件大小可以为 8 GB

python shell

1
推荐指数
1
解决办法
1913
查看次数

使用 Python 从多个文件夹中提取所有文件

我写下了这段代码:

import shutil

files = os.listdir(path, path=None)
for d in os.listdir(path):
    for f in files:
        shutil.move(d+f, path)
Run Code Online (Sandbox Code Playgroud)

我希望给定目录 ( path)中的每个文件夹都包含文件,将该文件夹中包含的文件移动到包含该文件夹的主目录 ( path) 中。

例如:此文件夹中的文件: C:/example/subfolder/ 将被移入: C:/example/

(并且该目录将被删除。)对不起,我的英语不好:)

python python-3.x

1
推荐指数
1
解决办法
2850
查看次数