如何避免os.system()在python中打印出返回值 [英] How to avoid os.system() printing out return value in python

查看:1930
本文介绍了如何避免os.system()在python中打印出返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python调用bash以执行另一个bash脚本:

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)

打印result后,它不仅给我输出,而且还给我返回状态(此处为0). 如果我只需要输出该怎么办?

还有,出于好奇,在Python中有多少种方法可以调用bash?如果有人可以给我一些使用它们的参考,我会感到很高兴,到目前为止,我只找到了os.system()os.popen().

After I printed result, it not only gives me the output but also the return status (0 here). What should I do if I only need the output?

And also, just for curiosity, how many ways are there to call bash in Python? I'll be glad if somebody can give me some references of how to use them, I've found only os.system() and os.popen() so far.

推荐答案

实际上,result只是返回状态,为整数.您正在调用的内容将写入stdout,它是从程序继承的,因此您会立即看到它被打印出.它永远不会对您的程序可用.

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

包括捕获输出,以及以不同方式调用shell.

Including capturing output, and invoking shells in different ways.

这篇关于如何避免os.system()在python中打印出返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆