我是否遗漏了一个步骤,使笔记本能够使用管道并在提供的文件上返回一个md5sum?
目前,它在我的计算机(Mac)上不能正常工作,下面的例子来自“Think”一书
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: 发布于 2020-05-22 14:24:15
在下面回答自己
我已经意识到这个问题不是木星笔记本本身。Mac不附带默认安装的md5sum,但是它提供了一个等效的工具md5。要计算文件的128位MD5散列,请运行以下命令:
md5 file.ext
该命令返回128位md5哈希。
上面最初使用的代码现在看起来如下所示。
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)https://stackoverflow.com/questions/61941030
复制相似问题