How do I create a subprocess in Python?

后端 未结 6 1587
孤独总比滥情好
孤独总比滥情好 2021-01-18 09:21

I would like to create a subprocess of a process.

What would be a working example which shows how to accomplish this?

6条回答
  •  终归单人心
    2021-01-18 10:22

    Start with the subprocess documentation.

    If you want to get the output:

    >>> import subprocess
    >>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]
    >>> output
    'Linux'
    

    If you just want to call and not deal with the output:

    >>> subprocess.call(['echo', 'Hi'])
    Hi
    0
    

    subprocess.check_call is the same except that it throws up a CalledProcessError in case the command is called with invalid parameters.

    A good subprocess tutorial.

提交回复
热议问题