问题
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:
- Looking at storage space at second usb stick, but this doesn't work beacause ISO image scans also unused space
- By adding
status=progress
todd
command I can get progress in terminal but I can't figure how to access stdout from python. I triedsubprocess.Popen,run(stdout = PIPE)
with and withoutshell = True
readingprocess.stdout
with.read()
,.read(1)
,.readline()
orcommunicate()
. 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