Home > Net >  Can I log a subprocess in Python?
Can I log a subprocess in Python?

Time:06-20

I have to lanch some apt install commands inside a Python script, and this is possible using the subprocess. But the problem is that I can't see the installation progress (error, completed, didn't start, ongoing etc.). Is there a way to print the apt logs just like in bash?

CodePudding user response:

Check out this example. It uses iter() over stdout from Popen. Since you didn't povide a code how do you call apt get I used ping for demo but the principle is the same.

import subprocess

process = subprocess.Popen(["ping", "-c", "3", "google.com"], stdout=subprocess.PIPE, universal_newlines=True)
for line in iter(process.stdout.readline, ""):
    print(line)
process.stdout.close()
return_code = process.wait()
  • Related