0

我试过这个:

import os

if os.system('somecommand') == 'output from command':

    do_something()

但它没有奏效。我该怎么做?我应该使用什么库?

当我使用下面的子流程解决方案时,我收到此错误代码:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
4

1 回答 1

3

的返回值os.system()是进程的退出代码,而不是它输出到标准输入的任何内容。您想改用该subprocess模块

import subprocess

if subprocess.check_output('somecommand') == 'output from command':
于 2012-11-11T13:51:25.493 回答