From this question which I asked a long time ago, what you may want to use is popen:
os.popen('cat /etc/services').read()
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 subprocess:
import subprocess
proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print("program output:", out)
Answer from Chris Bunch on Stack OverflowFrom this question which I asked a long time ago, what you may want to use is popen:
os.popen('cat /etc/services').read()
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 subprocess:
import subprocess
proc = 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 popen-type calls.
import subprocess
output = 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.
os.system() just runs the process, it doesn't capture the output:
If command generates any output, it will be sent to the interpreter standard output stream.
The return value is the exit code of the process:
On Unix, the return value is the exit status of the process encoded in the format specified for wait().
You'll need to use something like subprocess.check_output() or subprocess.Popen() directly to capture the output.
>>> arch = subprocess.check_output("uname -a | awk '{print $9}'", shell=True);
>>> arch
'x86_64\n'
You can use subprocess module and achieve this fairly easy.
#!/usr/bin/python3
import subprocess
getVersion = subprocess.Popen("awk '{print $7}' /etc/redhat-release", shell=True, stdout=subprocess.PIPE).stdout
version = getVersion.read()
print("My version is", version.decode())
Also for the awk part, you can use sed 's/[^0-9_.-]//g /etc/redhat-release. This will only extract the version number and doesn't need to know what field version number is which makes it more platform independant.
How to store os.system() output in a variable or a list in python - Stack Overflow
linux - reading output into variables inside the os.system command - Unix & Linux Stack Exchange
python - How to store output of os.system() in a variable - Stack Overflow
how to get the stdout of the script that os.system executes - Post.Byes
There are many good SO links on this one. try Running shell command from Python and capturing the output or Assign output of os.system to a variable and prevent it from being displayed on the screen for starters. In short
import subprocess
direct_output = subprocess.check_output('ls', shell=True) #could be anything here.
The shell=True flag should be used with caution:
From the docs: Warning
Invoking the system shell with shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.
See for much more info: http://docs.python.org/2/library/subprocess.html
you can use os.popen().read()
import os
out = os.popen('date').read()
print out
Tue Oct 3 10:48:10 PDT 2017
The os.system return the exit code of the command.
To capture the output of the command, you can use subprocess.check_output
output = subprocess.check_output('users', shell=True)
The value returned by the os.system is identical to the return value of the command you launched. Since most calls, like 'users' are written in C, they return 0 when the code is executed successfully ( they have a return 0; at the end of the main() ).
If you want to save their output, you can redirect their output path (by default stdout) to a text file, then read the text file.
user_name = os.system('users > users.txt')
login_details = os.system('w > w.txt')
with open("users.txt", "r") as f:
for line in f:
print line
with open("w.txt", "r") as f:
for line in f:
print line
os.system("rm users.txt")
os.system("rm w.txt")
I bow to the subprocess.check_output solution
If all you need is the stdout output, then take a look at subprocess.check_output():
Copyimport subprocess
batcmd="dir"
result = subprocess.check_output(batcmd, shell=True)
Because you were using os.system(), you'd have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell.
If you need to capture stderr as well, simply add stderr=subprocess.STDOUT to the call:
Copyresult = subprocess.check_output([batcmd], stderr=subprocess.STDOUT)
to redirect the error output to the default output stream.
If you know that the output is text, add text=True to decode the returned bytes value with the platform default encoding; use encoding="..." instead if that codec is not correct for the data you receive.
Copyimport subprocess
string="echo Hello world"
result=subprocess.getoutput(string)
print("result::: ",result)