subprocess.check_output return code

前端 未结 5 1848
刺人心
刺人心 2020-12-02 22:03

I am using:

grepOut = subprocess.check_output(\"grep \" + search + \" tmp\", shell=True)

To run a terminal command, I know that I can use a

5条回答
  •  一个人的身影
    2020-12-02 22:52

    Python 3.5 introduced the subprocess.run() method. The signature looks like:

    subprocess.run(
      args, 
      *, 
      stdin=None, 
      input=None, 
      stdout=None, 
      stderr=None, 
      shell=False, 
      timeout=None, 
      check=False
    )
    

    The returned result is a subprocess.CompletedProcess. In 3.5, you can access the args, returncode, stdout, and stderr from the executed process.

    Example:

    >>> result = subprocess.run(['ls', '/tmp'], stdout=subprocess.DEVNULL)
    >>> result.returncode
    0
    
    >>> result = subprocess.run(['ls', '/nonexistent'], stderr=subprocess.DEVNULL)
    >>> result.returncode
    2
    

提交回复
热议问题