By using module subprocess. It is included in Python's standard library and aims to be the substitute of os.system. (Note that the parameter capture_output of subprocess.run was introduced in Python 3.7)
>>> import subprocess
>>> subprocess.run(['cat', '/etc/hostname'], capture_output=True)
CompletedProcess(args=['cat', '/etc/hostname'], returncode=0, stdout='example.com\n', stderr=b'')
>>> subprocess.run(['cat', '/etc/hostname'], capture_output=True).stdout.decode()
'example.com\n'
In your case, just:
import subprocess
v = subprocess.run(['cat', '/etc/redhat-release'], capture_output=True).stdout.decode()
Update: you can split the shell command easily with shlex.split provided by the standard library.
>>> import shlex
>>> shlex.split('cat /etc/redhat-release')
['cat', '/etc/redhat-release']
>>> subprocess.run(shlex.split('cat /etc/hostname'), capture_output=True).stdout.decode()
'example.com\n'
Update 2: os.popen mentioned by @Matthias
However, is is impossible for this function to separate stdout and stderr.
import os
v = os.popen('cat /etc/redhat-release').read()
os.systemonly? To cite the documentation ofos.system: "Thesubprocessmodule provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function."os.system()has been deprecated for decades. One of the many reasons why is because it does not do what you want. Insisting on a legacy function that doesn't do what you want, just because you don't want to learn about how to do properly, is not a good strategy for success.