【发布时间】:2020-03-21 11:28:48
【问题描述】:
我想在我的 python 脚本中将“dd”命令的输出重定向到标准输出。这是我的代码:
def dd2pipe():
chunk=654321
skip_ntimes= 2
read_ntimes= 3
filepath='39476a90-a5f1-cd59-7a8d-34c016276514.high.mp3'
p1_cmds = [f'dd bs={chunk}',
f'skip={skip_ntimes}',
f'count={read_ntimes}',
f'if={filepath}']
p1 = sp.Popen(p1_cmds,shell=True, stdout=sp.PIPE, stderr=sp.STDOUT)
byte_data= p1.stdout.read()
p1.stdout.close()
p1.wait()
print(byte_data)
dd2pipe()
我得到的输出是:
b'0+0 records in\n0+0 records out\n0 bytes copied, 2,338e-05 s, 0,0 kB/s\n'
你能帮我在 STDOUT 中读取 dd 命令的字节吗?
【问题讨论】:
-
请注意
dd和bs={chunk}如何在同一个数组元素中,但其他参数不是。您应该将其更改为在一个方向或另一个方向上保持一致,并相应地选择shell=True或shell=False。 -
当您在 Python 脚本之外手动执行相同的
dd命令时会发生什么?直接在命令行上? -
你为什么还要使用
dd?只需使用 Python 读取字节即可。 -
@MarkSetchell 使用
dd可能比在 Python 中重新实现其功能更简洁(也可能更高效)。如果您将其视为 DSL,我认为收益大于启动新流程的成本。
标签: python subprocess stdout dd