这是我的代码,但它不起作用。执行dd命令,但不打印输出。注意:如果我将标准输出更改为常规文本文件,则dd输出将逐步保存在dd打印输出的每个进度行的文件中。
想法?问候。
import sys
from subprocess import Popen, STDOUT, PIPE
with Popen(["dd", "if=/dev/cdrom", "of=/tmp/prova.iso", "bs=2048", "count=499472", "status=progress"], stderr=STDOUT, stdout=PIPE) as proc:
print("ok")
print(proc.stdout.read())
我找到了解决方案。
import subprocess
import sys
cmd = ["dd", "if=/dev/cdrom", "of=/tmp/iso.iso", "bs=2048", "count=499472", "status=progress"]
process = subprocess.Popen(cmd, stderr=subprocess.PIPE)
line = ''
while True:
out = process.stderr.read(1)
if out == '' and process.poll() != None:
break
if out != '':
s = out.decode("utf-8")
if s == '\r':
print(line)
line = ''
else:
line = line + s
谢谢大家的答案。