2

this is my code but it doesn't works. dd command is executed, but no output is printed out. Note: if I change the stdout to a regular text file, the dd output is progressively saved in the file at every progress line that dd prints out.

Ideas? Regards.

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())
felice.murolo
  • 146
  • 2
  • 9
  • I'll leave you with [this](https://gist.github.com/Torxed/581a6cda6d6b97541509f3ca258af042) which shows how you can print data when it's available at each line break/carriage return. Giving a answer to this question would be pretty broad and there's already topics on SO about this particular matter. – Torxed Feb 24 '19 at 21:51
  • There's also this post: https://stackoverflow.com/questions/1388753/how-to-get-output-from-subprocess-popen-proc-stdout-readline-blocks-no-dat – CryptoFool Feb 24 '19 at 21:56
  • Thanks @Torxed, I've get the cmd.py. I thought that, by running cmd.py in a shell, I could see printed the result of "ls -l", which seems to be the command that is launched by default. However, when I launch cmd.py, nothing happens. What is wrong? – felice.murolo Feb 25 '19 at 17:50
  • Thank you @Steve. I've found a solution with this link. – felice.murolo Feb 25 '19 at 18:21

2 Answers2

2

I've found a solution.

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

Thank you all for your answers.

felice.murolo
  • 146
  • 2
  • 9
1

Take a look at this: dd with progress in python

Community
  • 1
  • 1
Mahrez BenHamad
  • 1,791
  • 1
  • 15
  • 21