Getting stdout from console

醉酒当歌 提交于 2020-05-17 05:55:48

问题


I'm trying to make a python script that would clone(ISO image) one usb stick to another using dd if=/dev/sda of=/dev/sdb

Here's my problem: I want to create progress bar showing what is done.

I tried:

  1. Looking at storage space at second usb stick, but this doesn't work beacause ISO image scans also unused space
  2. By adding status=progress to dd command I can get progress in terminal but I can't figure how to access stdout from python. I tried subprocess.Popen,run(stdout = PIPE) with and without shell = True reading process.stdout with .read(), .read(1), .readline() or communicate(). Nothing worked for me.(https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python)

I can see progress going on in python shell but .read() function always get stuck.

Part of code I am concerned about:

comm = 'sudo dd if=/dev/sda of=/dev/sdb'
cloning = subprocess.Popen(shlex.split(comm),stdout = PIPE,text = True)
while True:
    print(cloning.stdout.read())

I want something that would work like:

while True:
    progress = cloning.stdout.read()
    update_bar(progress)

I'm using python 3.7 on Raspberry

Thanks for help


回答1:


You were on the right track with status=progress, but it outputs to stderr, not stdout. If you do stderr = PIPE and then read from cloning.stderr instead of cloning.stdout, it will work.



来源:https://stackoverflow.com/questions/61688714/getting-stdout-from-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!