Return value of x = os.system(..)
os.system()
returns the (encoded) process exit value. 0
means success:
On Unix, the return value is the exit status of the process encoded in the format specified for
wait()
. Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
The output you see is written to stdout
, so your console or terminal, and not returned to the Python caller.
If you wanted to capture stdout
, use subprocess.check_output()
instead:
x = subprocess.check_output(['whoami'])
What is the return value of os.system() in Python?
The return value of os.system
is OS-dependant.
On Unix, the return value is a 16-bit number that contains two different pieces of information. From the documentation:
a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero)
So if the signal number (low byte) is 0, it would, in theory, be safe to shift the result by 8 bits (result >> 8
) to get the error code. The function os.WEXITSTATUS
does exactly this. If the error code is 0, that usually means that the process exited without errors.
On Windows, the documentation specifies that the return value of os.system
is shell-dependant. If the shell is cmd.exe
(the default one), the value is the return code of the process. Again, 0 would mean that there weren't errors.
For others error codes:
- on Linux
- on Windows
How to store the return value of os.system that it has printed to stdout in python?
import subprocess
p = subprocess.Popen('my_command', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, error = p.communicate()
Assign output of os.system to a variable and prevent it from being displayed on the screen
From "Equivalent of Bash Backticks in Python", 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
How to get the output from os.system()?
Use subprocess
:
import subprocess
print(subprocess.check_output(['nslookup', 'google.com']))
If the return code is not zero it will raise a CalledProcessError
exception:
try:
print(subprocess.check_output(['nslookup', 'google.com']))
except subprocess.CalledProcessError as err:
print(err)
os.system only returns the exit code of the command. Here 0
means success. Any other number stands for an operating-system-dependent error. The output goes to stdout of this process. subprocess intends to replace os.system
.
subprocess.check_output is a convenience wrapper around subprocess.Popen that simplifies your use case.
OS.system- adding the output of OS .system output command to the command of another os system command
os.system
does not return the output of the command - it returns the errorlevel.
If you need command output, use
wwn = os.popen(var).read()
That will assign the output from command var
to wwn
.
Be warned - the output is returned completely, with the trailing newline. You might want to strip()
it before using it.
How to hide or delete the 0 when call to `os.system` in Python?
What about this?
from subprocess import check_output
user_name = check_output('whoami').strip()
print user_name
#out: userX
What does the 0 stand for at the end of the text which os.system() returns?
os.system
returns the exit status of the process you executed. Since the cat
command succeeded, it's exit code is 0, and that's what you see from print(x)
.
Python: How to get stdout after running os.system?
If all you need is the stdout
output, then take a look at subprocess.check_output()
:
import 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:
result = 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.
How to return a value using a C file, then pick it up in python using the os.system?
According to os.system
documentation:
On Unix, the return value is the exit status of the process encoded in
the format specified forwait()
. Note that POSIX does not specify the
meaning of the return value of the C system() function, so the return
value of the Python function is system-dependent.
Use os.WEXITSTATUS
to get the return code:
import os
x = os.system("./a.out")
print os.WEXITSTATUS(x)
Related Topics
How to Get Linux Console Window Width in Python
Linux Command-Line Call Not Returning What It Should from Os.System
How to Use Export With Python on Linux
What Does If _Name_ == "_Main_": Do
How to Count the Occurrences of a List Item
String Formatting: % Vs. .Format Vs. F-String Literal
Pip' Is Not Recognized as an Internal or External Command
Rolling or Sliding Window Iterator
Understanding Generators in Python
Fatal Error: Python.H: No Such File or Directory
Why Does Multiprocessing Use Only a Single Core After I Import Numpy
Tkinter.Photoimage Doesn't Not Support Png Image
Trouble Installing Scipy in Virtualenv on a Amazon Ec2 Linux Micro Instance
Why Does "A == X or Y or Z" Always Evaluate to True
Are Global Variables Thread-Safe in Flask? How to Share Data Between Requests
How to Provide a Reproducible Copy of Your Dataframe With To_Clipboard()