Assign output of os.system to a variable and prevent it from being displayed on the screen [duplicate]
From "Equivalent of Bash Backticks in Python", which I asked a long time ago, what you may want to use is
From the docs for Python 3.6,
This is implemented using subprocess.Popen; see that class’s documentation for more powerful ways to manage and communicate with subprocesses.
Here's the corresponding code for
import subprocessproc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)(out, err) = proc.communicate()print "program output:", out
You might also want to look at the
subprocess module, which was built to replace the whole family of Python
import subprocessoutput = subprocess.check_output("cat /etc/services", shell=True)
The advantage it has is that there is a ton of flexibility with how you invoke commands, where the standard in/out/error streams are connected, etc.
The commands module is a reasonably high-level way to do this:
import commandsstatus, output = commands.getstatusoutput("cat /etc/services")
status is 0, output is the contents of /etc/services.