为什么python子进程中的“dd”不向标准输出写入字节?

2024-09-27 07:29:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python脚本中将“dd”命令的输出重定向到stdout。这是我的代码:

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命令的字节吗


Tags: 命令readdatastdoutbytespddcmds
1条回答
网友
1楼 · 发布于 2024-09-27 07:29:14

多亏了Ry-s的回答,它才有效。我将shell更改为False,将p1_cmds更改为:

p1_cmds = ['dd',                 
           f'bs={chunk}', 
           f'skip={skip_ntimes}', 
           f'count={read_ntimes}', 
           f'if={filepath}']

p1 = sp.Popen(p1_cmds,shell=False, stdout=sp.PIPE, stderr=sp.STDOUT)

相关问题 更多 >

    热门问题