What is the return value of os.system() in Python?

PythonOperating System

Python Problem Overview


I came across this:

>>> import os
>>> os.system('ls')
file.txt README
0

What is return value of os.system()? Why I get 0?

Python Solutions


Solution 1 - 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:

Solution 2 - Python

os.system('command') returns a 16 bit number, which first 8 bits from left(lsb) talks about signal used by os to close the command, Next 8 bits talks about return code of command.

00000000    00000000
exit code   signal num

Example 1 - command exit with code 1

os.system('command') #it returns 256
256 in 16 bits -  00000001 00000000
Exit code is 00000001 which means 1

Example 2 - command exit with code 3

os.system('command') # it returns 768
768 in 16 bits  - 00000011 00000000
Exit code is 00000011 which means 3

Now try with signal - Example 3 - Write a program which sleep for long time use it as command in os.system() and then kill it by kill -15 or kill -9

os.system('command') #it returns signal num by which it is killed
15 in bits - 00000000 00001111
Signal num is 00001111 which means 15

You can have a python program as command = 'python command.py'

import sys
sys.exit(n)  # here n would be exit code

In case of c or c++ program you can use return from main() or exit(n) from any function #

Note - This is applicable on unix

> 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. > > > os.wait() > > Wait for completion of a child process, and return a tuple containing its pid and exit status indication: 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); the high bit > of the low byte is set if a core file was produced. > > Availability: Unix

.

Solution 3 - Python

You might want to use

return_value = os.popen('ls').read()

instead. os.system only returns the error value.

The os.popen is a neater wrapper for subprocess.Popen function as is seen within the python source code.

Solution 4 - Python

> "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."

http://docs.python.org/library/os.html#os.system

There is no error, so the exit code is zero

Solution 5 - Python

os.system() returns some unix output, not the command output. So, if there is no error then exit code written as 0.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser813713View Question on Stackoverflow
Solution 1 - PythonrubikView Answer on Stackoverflow
Solution 2 - PythonAlokThakurView Answer on Stackoverflow
Solution 3 - Pythonuser2589273View Answer on Stackoverflow
Solution 4 - PythoncEzView Answer on Stackoverflow
Solution 5 - PythonsamView Answer on Stackoverflow