How to avoid os.system() printing out return value in python

前端 未结 4 892
一整个雨季
一整个雨季 2021-01-20 18:29

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)


        
相关标签:
4条回答
  • 2021-01-20 18:56

    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.

    0 讨论(0)
  • 2021-01-20 19:06

    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)
    
    0 讨论(0)
  • 2021-01-20 19:12

    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.

    0 讨论(0)
  • 2021-01-20 19:13

    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.

    0 讨论(0)
提交回复
热议问题