To save the output of a command to a file, you can use subprocess.check_call() :
from subprocess import STDOUT, check_call with open("/tmp/dpkg.txt", "wb") as file: check_call(["sudo", "dpkg", "-l"], stdout=file, stderr=STDOUT)
stderr=STDOUT used to redirect stderr commands to stdout.
source share