【发布时间】:2015-07-09 09:36:11
【问题描述】:
Generating an MD5 checksum of a file
我使用了上面链接中的一个示例,但通过删除对函数的调用(最初来自上面)对其进行了一些小改动:
import hashlib
hash = hashlib.md5()
with open('validFilePath/file.xxx', "r+b") as f:
for block in iter(lambda: f.read(65536), ""):
hash.update(block)
return hash.hexdigest()
这给了我以下错误。
File "<string>", line 4, in <module>
TypeError: 'function' object is not iterable
我错过了一些基本的 Python 前提吗?
【问题讨论】:
-
@jonrsharpe: 这是
iter()的有效用法,请参阅What's perfect counterpart in Python for "while not eof" 了解将可调用对象传递给iter()的另一个示例。 -
发布您的实际代码,特别是第 4 行。
-
你能发布你的实际代码吗?
-
你确定你有
iter()的第二个参数吗?例如,我可以使用iter(lambda: f.read(65536))重现您的错误消息。 -
@Padraic 我如何保留回报?这是我的实际代码: python -c "import hashlib hash = hashlib.md5() with open('/Volumes/Macintosh HD/AF 1/bbb.def', 'r+b') as f: for block in iter (lambda: f.read(65536), ""): hash.update(block) print hash.hexdigest()"