What Is the Return Value of Os.System() in Python

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

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 does os.system return and how can I convert its output?

You probably need subprocess.check_output() as it allows you to

Run command with arguments and return its output as a byte string.

How to store output of os.system() in a variable

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)

Is there a way to test the results of an os.system() command in Python?

The return of the os.system function is the return the operating system program (or command) does. Conventionally, it is 0 in case of success and a a number different from 0 if it fails. The error message that you want is not the result of the command, but what is sent to stderr (typically).

Thus, your code should be:

import os
import re

while True:
com = input()

if com == "break":#Doesn't matter
break

ret_value = os.system(com)
if ret_value != 0:
print(f"Error. Command returned {ret_value}")
else:
print("Command returned success")

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

Python os.system() returning bad value

So the short answer is that the value I'm getting is the exit code from the program, unless Windows feels it has something important to say.

In this case a DLL I was calling was corrupting the heap and cause windows to abort the execution. To get the actual 32 bit Windows return code and not a sign extended version I needed to AND it with 0xffffffff. I don't know if I can answer my own question, but gelonida set me on the right trail the answer.

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.

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()

python os.system command line return value back to python

You can use the subprocess module and redirect its output through a pipe.

For example, to get the list of file in current directory.

import subprocess
proc = subprocess.Popen(['ls'], stdout=subprocess.PIPE)
print(proc.stdout.readlines())

More details here Redirecting stdio from a command in os.system() in Python

Edit: if you are trying to pass arguments to subprocess.Popen each one gets its own set of quotes.

proc = subprocess.Popen(['python','test.py','-b','-n'], stdout=subprocess.PIPE)

And the doc https://docs.python.org/2/library/subprocess.html



Related Topics



Leave a reply



Submit