Skip to content Skip to sidebar Skip to footer

How To Avoid Os.system() Printing Out Return Value In Python

I'm using Python to call bash to execute another bash script: begin = int(sys.argv[1]) result = os.system('/tesladata/isetools/cdISE.bash %s' %begin) After I printed result, it no

Solution 1:

Actually, result is only the return status as an integer. The thing you're calling writes to stdout, which it inherits from your program, so you're seeing it printed out immediately. It's never available to your program.

Check out the subprocess module docs for more info:

http://docs.python.org/library/subprocess.html

Including capturing output, and invoking shells in different ways.


Solution 2:

You can just throw away any output by piping to /dev/null.

begin = int(sys.argv[1])
result = os.system("/tesladata/isetools/cdISE.bash %s > /dev/null" %begin)

If you don't want to display errors either, change the > to 2&> to discard stderr as well.


Solution 3:

Your python script does not have the output of the bash script at all, but only the "0" returned by it. The output of the bash script went to the same output stream as the python script, and printed before you printed the value of result. If you don't want to see the 0, do not print result.


Solution 4:

you can use the subprocess module like this assign it to a var (x for ex)

import subprocess
x = subprocess.run('your_command',capture_output=True)

Post a Comment for "How To Avoid Os.system() Printing Out Return Value In Python"