如何在python中从os.system中删除0表单?
我想知道如何在python中从如何在python中从os.system中删除0表单?,python,python-2.7,Python,Python 2.7,我想知道如何在python中从os.system中删除0,例如,在这个变量中,我可以删除它而不会出现任何问题: user_name = check_output('whoami').strip() output = current-user 但问题是当我运行这个变量时: from subprocess import check_output subtests = os.system('/home/' + user_name + '/tests/cfile --list-subtests')
os.system
中删除0,例如,在这个变量中,我可以删除它而不会出现任何问题:
user_name = check_output('whoami').strip()
output = current-user
但问题是当我运行这个变量时:
from subprocess import check_output
subtests = os.system('/home/' + user_name + '/tests/cfile --list-subtests')
output = subtests 1
subtests 2
0
如果我在第一个命令中应用相同的命令结构
subtests = check_output('/home/' + user_name + '/intel-graphics/intel-gpu-tools/tests/' + line + ' --list-subtests').strip()
它不工作,我得到了一些python的错误,我研究了很多,但到目前为止,我还没有找到一个很好的修复方法
注意:我正在使用python 2.7执行命令
子进程。check_output()
返回一个字符串,其中包含进程的输出。因此,您可以应用字符串方法os.system()
不返回字符串,而是返回状态,它是一个整数——您不能strip()
一个整数
您看到的“输出”是进程输出,它直接转储到屏幕上。如果要使用它,请使用
检查输出()。这就是它的用途。当您调用os.system
时,它会启动一个新进程。该进程的标准输入和输出流连接到Python程序流
也就是说,当您调用os.system
时,无论子进程输出写入到终端(或主程序输出到哪里)。就这样,
>>> result = os.system(".../cfile --list-subtests")
subtests 1
subtests 2
>>> result
0
上面的“subtests 1”/“subtests 2”行只是从子进程传递到终端的文本。它从来没有被Python处理过,也没有被转到任何变量。返回值仅为0,这通常意味着操作已成功完成
如果要捕获输出使用,请检查输出(与第一个示例中使用的完全相同!)或通用的Popen
:
>>> process = subprocess.Popen([".../cfile", "--list-subtests"], stdout=subprocess.PIPE)
>>> process_out, process_err = process.communicate()
>>> process_out
'subtests 1\nsubtests 2\n'
>>> process.returncode
0
试一试
commands.getstatusoutput("your command")
它返回一个元组。然后您可以使用您想要的任何输出您给出的代码根本不起作用(例如,当前用户
作为变量或子测试1
以及以下内容)。请发布一些我们可以执行的代码。嗯,我不明白你的意思,你能给我举个例子吗