felice.murolo
felice.murolo

Reputation: 166

Running dd from python and getting progress

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())

Upvotes: 3

Views: 8667

Answers (2)

felice.murolo
felice.murolo

Reputation: 166

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.

Upvotes: 3

Mahrez BenHamad
Mahrez BenHamad

Reputation: 2068

Take a look at this: dd with progress in python

Upvotes: 2

Related Questions