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:
subprocess.Popen with stdout=PIPE and stderr=PIPEThis allows you to read the output in real-time and print it as the command progresses.
pythonimport 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.
subprocess.run with stdout and stderr Pipes (Python 3.7+)This is a more modern approach and achieves the same result:
pythonimport subprocess cmd = ['adb', 'push', 'folder/', '/sdcard/'] process = subprocess.run(cmd, stderr=subprocess.PIPE, universal_newlines=True) for line in process.stderr.splitlines(): print(line)
tqdm for a Progress BarIf you want a visual progress bar, you can parse the output of adb push and use the tqdm library to display progress.
pythonimport 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()
adb with --sync and Parse OutputFor 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.