I'm really struggling to successfully stream output from a bash command in my Python script using the subprocess
package. The command is just a simple AWS CLI
command to upload an object to S3:
aws s3 cp --profile MY-PROFILE UPLOADED_FILE s3://BUCKET/PREFIX/
When I run this in bash
, it shows the progress of the upload. With subprocess
, I can only get it working in the following ways:
Final output only at the end.
upload: ./OBJECT to s3://BUCKET/PREFIX/OBJECT
Line by line output
Completed 512.0 KiB/12.2 MiB (697.8 KiB/s) with 1 file(s) remaining Completed 768.0 KiB/12.2 MiB (980.0 KiB/s) with 1 file(s) remaining Completed 1.0 MiB/12.2 MiB (1.2 MiB/s) with 1 file(s) remaining Completed 1.2 MiB/12.2 MiB (1.5 MiB/s) with 1 file(s) remaining Completed 1.5 MiB/12.2 MiB (1.6 MiB/s) with 1 file(s) remaining Completed 1.8 MiB/12.2 MiB (1.4 MiB/s) with 1 file(s) remaining
Similar thing without line separation:
b'Completed 256.0 KiB/12.2 MiB (297.5 KiB/s) with 1 file(s) remaining\\rCompleted 512.0 KiB/12.2 MiB (506.7 KiB/s) with 1 file(s) remaining\\rCompleted 768.0 KiB/12.2 MiB (544.4 KiB/s) with 1 file(s) remaining\\rCompleted 1.0 MiB/12.2 MiB (672.0 KiB/s) with 1 file(s) remaining \\rCompleted 1.2 MiB/12.2 MiB (707.0 KiB/s) with 1 file(s) remaining \\rCompleted 1.5 MiB/12.2 MiB (780.3 KiB/s) with 1 file(s) remaining \\rCompleted 1.8 MiB/12.2 MiB (849.1 KiB/s) with 1 file(s) remaining \\rCompleted 2.0 MiB/12.2 MiB (865.6 KiB/s) with 1 file(s) remaining \\rCompleted 2.2 MiB/12.2 MiB (971.0 KiB/s) with 1 file(s) remaining \\rCompleted 2.5 MiB/12.2 MiB (992.4 KiB/s) with 1 file(s) remaining \\rCompleted 2.8 MiB/12.2 MiB (1.0 MiB/s) with 1 file(s) remaining
What I need is to just use one line and show the live status, just as it would do in bash.
Here are a number of different functions I've tried, all with no luck:
from subprocess import Popen, PIPE
import subprocess
import os
import shlex
from time import sleep
def stream_process(process):
go = process.poll() is None
for line in process.stdout:
print(line)
return go
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while stream_process(process):
sleep(0.1)
def run(command):
with subprocess.Popen(command, text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as process:
for line in process.stdout:
print(line)
run(command)
process = Popen(command, text=True, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True)
while True:
output = process.stdout.readline()
if process.poll() is not None and output == b'':
break
if output:
print (output.strip())
retval = process.poll()
with Popen(command, text=True, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
while True:
line = p.stdout.readline()
print(line.strip())
for line in p.stdout:
print(line.strip(), "\n", flush=True, end='')
# print(line.strip())
print(result.stdout.readlines())
print("args", result.args)
print("returncode", result.returncode)
print("stdout", result.stdout)
print("stderr", result.stderr)
print("check_returncode()", result.check_returncode())
def run_command(cmd):
p = Popen(shlex.split(cmd), bufsize=1, universal_newlines=True)
return p.poll()
run_command(command)
# invoke process
process = subprocess.Popen(shlex.split(command),shell=False,stdout=subprocess.PIPE)
# Poll process.stdout to show stdout live
while True:
output = process.stdout.readline()
if process.poll() is not None:
break
if output:
print(output.strip())
rc = process.poll()
print(result.stdout.readlines())
while True:
line = result.stdout.readline()
if not line:
break
print(line, flush=True)
FAQ
Why don't you use
boto3
instead ofsubprocess
+bash
?
I'm developing a program for non-Python devs, so I'd like to limit dependencies and only use standard packages as much as possible.
You simply should to:
import subprocess
p = subprocess.Popen(("ls"), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout:
print(line.decode("utf-8").rstrip())
p.wait()
status = p.poll()
print("process terminate with code: %s" % status)
Result in:
my-file.txt
...
process terminate with code: 0
When the subprocess exit, stdout is closed, which break the loop.
subprocess stdout is a bytes stream. If you need str objects, you should decode it: line.decode("utf-8")
.
But I think this is not the problem. Some command have different output in case of stdout is a terminal or a pipe. Like with wget
. I you want a progress bar, you have to parse output and implement your own progress bar.
for line in process.stdout
and process.stdout.readline()
are not fit for the task because they don't return before a whole line has ended, ie a \\n
is received, while a progress part only ends with \\r
. A way to read output independently of any line termination is to use os.read() :
from os import read
process = Popen(shlex.split(cmd), stdout=PIPE)
while buffer := read(process.stdout.fileno(), 4096):
print(buffer.decode(), end='')
As xrisk noted, using text=True
would allow lines ending with \\r
to be read with readline()
, but that's of no use here because therewith all line endings in the output will be converted to '\\n'
.
But there's indeed a way to use a text stream without loosing the original line endings by means of io.TextIOWrapper
- therewith we can do this:
import io
process = Popen(shlex.split(cmd), stdout=PIPE)
for line in io.TextIOWrapper(process.stdout, newline=""): print(line, end="")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.