How to display the progress of Python `subprocess.check_output('adb push folder/ /sdcard/')`?
Feb 13, 7:31pm

When you run subprocess.check_output('adb push folder/ /sdcard/') in Python, the command executes silently by default—it only returns the output after completion, which isn't ideal for tracking progress. To display the progress of the adb push command, you have a few options:


1. Use subprocess.Popen with stdout=PIPE and stderr=PIPE

This allows you to read the output in real-time and print it as the command progresses.

python
import subprocess

cmd = ['adb', 'push', 'folder/', '/sdcard/']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

for line in process.stderr:  # adb push writes progress to stderr
    print(line, end='')

process.wait()

Why stderr?
The adb push command writes its progress to stderr, not stdout.


2. Use subprocess.run with stdout and stderr Pipes (Python 3.7+)

This is a more modern approach and achieves the same result:

python
import subprocess

cmd = ['adb', 'push', 'folder/', '/sdcard/']
process = subprocess.run(cmd, stderr=subprocess.PIPE, universal_newlines=True)

for line in process.stderr.splitlines():
    print(line)

3. Use tqdm for a Progress Bar

If you want a visual progress bar, you can parse the output of adb push and use the tqdm library to display progress.

python
import subprocess
from tqdm import tqdm

cmd = ['adb', 'push', 'folder/', '/sdcard/']
process = subprocess.Popen(cmd, stderr=subprocess.PIPE, universal_newlines=True)

for line in process.stderr:
    if '%' in line:  # Example: "file.txt: 100% (1/1)"
        print(line, end='')
    elif 'KB/s' in line:  # Example: "[ 50%] 1000 KB/s"
        print(line, end='')

process.wait()

4. Use adb with --sync and Parse Output

For more granular control, you can use the adb sync command, but this is more complex and less common for simple file pushes.


Key Takeaway:
The simplest and most effective way is to use subprocess.Popen and read from stderr in real-time. This will show you the progress as adb push reports it.

Feb 13, 7:31pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.