24小时热门版块排行榜    

查看: 1038  |  回复: 11
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

渔夫之友

铁虫 (小有名气)

[求助] 程序运行出差错 求解。 已有1人参与

import hashlib
import sys
def multi_hash(filename):
    """Calculates the md5 and sha256 hashes
    of the specified file and returns a list
    containing the hash sums as hex strings."""
    md5=hashlib.md5()
    sha256=hashlib.sha256()
    with open(filename,'rb') as f:
        while True:
            buf=f.read(2**20)
            if not buf :
                break
            md5.update(buf)
            sha256.update(buf)
        return [md5.hexdigest(),sha256.hexdigest()]
if __name__ == '__main__':
        hashes=[]
        print'-------MD5 sums--------'
        for filename in sys.argv[1:]:
            h=multi_hash(filename)
            hashes.append(h)
            print '%s %s' % (h[0],filename)
        print '--------SHA256 sums--------'
        for i in range (len(hashes)):
            print '%s %s' % (hashes[1],sys.argv[i+1])

这是一本书上计算文本hash值的代码,书上的运行结果如第一图。而我运行起来却是第二图。这是何故?

程序运行出差错 求解。
818608499699580697.jpg


程序运行出差错 求解。-1
163D.tmp.png
回复此楼

» 猜你喜欢

青春的未来
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

kunis

铁虫 (初入文坛)

【答案】应助回帖

感谢参与,应助指数 +1
import hashlib
import sys
def multi_hash(filename):
    """Calculates the md5 and sha256 hashes
    of the specified file and returns a list
    containing the hash sums as hex strings."""
    md5=hashlib.md5()
    sha256=hashlib.sha256()
    with open(filename,'rb') as f:
        while True:
            buf=f.read(2**20)
            if not buf :
                break
            md5.update(buf)
            sha256.update(buf)
        return [md5.hexdigest(),sha256.hexdigest()]
if __name__ == '__main__':
        hashes=[]
        print'-------MD5 sums--------'
        for filename in sys.argv[1:]:
            h=multi_hash(filename)
            hashes.append(h)
            print '%s %s' % (h[0],filename)
        print '--------SHA256 sums--------'
        for i in range (len(hashes)):
            print '%s %s' % (hashes[1],sys.argv[i+1]) # 这行修改一下



python mutilhash.py /bin/{ls,bash,sh}
-------MD5 sums--------
b691e28e120f6989e37c7db21cb51931 /bin/ls
144968564a6b1159ed82059920cea76f /bin/bash
ced2ebfccfe2d52f362908020acd5831 /bin/sh
--------SHA256 sums--------
1e87d99599ddea2a93f060b50a54066e8b756d752158e6147cbb99b06eb11d99 /bin/ls
a186368e1b73be93a2674201124abb1fb16489812ef4aa4431004f0065d9e94b /bin/bash
103e9972afdbe01061291137705183ea5c91b3a6ba07c22a9c0a50d2eab97bcc /bin/sh
4楼2016-10-20 00:29:14
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 12 个回答

渔夫之友

铁虫 (小有名气)

咋没人帮帮忙呢,急等啊

发自小木虫Android客户端
青春的未来
2楼2016-10-18 21:48:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

渔夫之友

铁虫 (小有名气)

青春的未来
3楼2016-10-19 07:49:57
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

kunis

铁虫 (初入文坛)

【答案】应助回帖

print '%s %s' % (hashes[1],sys.argv[i+1]) # 这行修改一下
为:
print '%s %s' % (hashes[1],sys.argv[i+1])
5楼2016-10-20 00:30:59
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 312求调剂 +4 上岸吧ZJY 2026-03-22 6/300 2026-03-25 19:09 by 星空星月
[考研] 086000生物与医药292求调剂 +4 小小陈小小 2026-03-22 7/350 2026-03-25 19:07 by 星空星月
[考研] 【2026考研调剂】制药工程 284分 求相关专业调剂名额 +4 袁奂奂 2026-03-25 8/400 2026-03-25 14:32 by lbsjt
[考研] 081700 调剂 267分 +10 迷人的哈哈 2026-03-23 10/500 2026-03-25 12:12 by userper
[考研] 0854电子信息求调剂 324 +4 Promise-jyl 2026-03-23 4/200 2026-03-25 11:36 by Sugarlight
[考研] 086003食品工程求调剂 +6 淼淼111 2026-03-24 6/300 2026-03-25 10:29 by 3Strings
[考研] 07化学280分求调剂 +7 722865 2026-03-23 7/350 2026-03-25 09:29 by aa331100
[考研] 化学调剂 +6 yzysaa 2026-03-21 6/300 2026-03-25 09:27 by aa331100
[考研] 291求调剂 +3 HanBeiNingZC 2026-03-24 3/150 2026-03-24 16:34 by barlinike
[考研] 材料292调剂 +8 橘颂思美人 2026-03-23 8/400 2026-03-24 16:33 by laoshidan
[考博] 申博26年 +4 八6八68 2026-03-19 4/200 2026-03-24 15:49 by 小Ben呵呵
[考研] 307求调剂 +5 超级伊昂大王 2026-03-24 5/250 2026-03-24 15:46 by 星空星月
[考研] 277分求调剂,跨调材料 +3 考研调剂lxh 2026-03-24 3/150 2026-03-24 13:52 by JourneyLucky
[考研] 315分,诚求调剂,材料与化工085600 +3 13756423260 2026-03-22 3/150 2026-03-22 20:11 by edmund7
[考研] 生物学调剂 +5 Surekei 2026-03-21 5/250 2026-03-22 14:39 by tcx007
[考研] 一志愿南大,0703化学,分数336,求调剂 +3 收到VS 2026-03-21 3/150 2026-03-21 18:42 by 学员8dgXkO
[考研] 266求调剂 +3 哇呼哼呼哼 2026-03-20 3/150 2026-03-21 16:46 by barlinike
[考研] 22 350 本科985求调剂,求老登收留 +3 李轶男003 2026-03-20 3/150 2026-03-21 13:28 by 搏击518
[考研] 330求调剂0854 +3 assdll 2026-03-21 3/150 2026-03-21 13:01 by 搏击518
[考研] 中南大学化学学硕337求调剂 +3 niko- 2026-03-19 6/300 2026-03-20 21:58 by luoyongfeng
信息提示
请填处理意见