首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >木星笔记本和md5sum是否存在已知的问题?

木星笔记本和md5sum是否存在已知的问题?
EN

Stack Overflow用户
提问于 2020-05-21 18:10:37
回答 1查看 95关注 0票数 0

我是否遗漏了一个步骤,使笔记本能够使用管道并在提供的文件上返回一个md5sum?

目前,它在我的计算机(Mac)上不能正常工作,下面的例子来自“Think”一书

代码语言:javascript
复制
def pipe(cmd):
     """Runs a command in a subprocess.

     cmd: string Unix command

     Returns (res, stat), the output of the subprocess and the exit status.
"""
     # Note: os.popen is deprecated
     # now, which means we are supposed to stop using it and start using
     # the subprocess module.  But for simple cases, I find
     # subprocess more complicated than necessary.  So I am going
     # to keep using os.popen until they take it away.

     fp = os.popen(cmd)
     res = fp.read()
     stat = fp.close()
     assert stat is None
     return res, stat

cmd= 'md5sum ' + 'words.txt'
pipe(cmd)

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-137-ccd8fd0ce9f8> in <module>
     18     return res, stat
     19 cmd= 'md5sum ' + 'words.txt'
---> 20 pipe(cmd)

<ipython-input-137-ccd8fd0ce9f8> in pipe(cmd)
     15     res = fp.read()
     16     stat = fp.close()
---> 17     assert stat is None
     18     return res, stat
     19 cmd= 'md5sum ' + 'words.txt'

AssertionError: 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-22 14:24:15

在下面回答自己

我已经意识到这个问题不是木星笔记本本身。Mac不附带默认安装的md5sum,但是它提供了一个等效的工具md5。要计算文件的128位MD5散列,请运行以下命令:

md5 file.ext

该命令返回128位md5哈希。

上面最初使用的代码现在看起来如下所示。

代码语言:javascript
复制
def pipe(cmd):
     """Runs a command in a subprocess.

     cmd: string Unix command

     Returns (res, stat), the output of the subprocess and the exit status.
"""
     # Note: os.popen is deprecated
     # now, which means we are supposed to stop using it and start using
     # the subprocess module.  But for simple cases, I find
     # subprocess more complicated than necessary.  So I am going
     # to keep using os.popen until they take it away.

     fp = os.popen(cmd)
     res = fp.read()
     stat = fp.close()
     assert stat is None
     return res, stat

cmd= 'md5 ' + 'words.txt'
pipe(cmd)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61941030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档