How do I create a subprocess in Python?

后端 未结 6 1550
孤独总比滥情好
孤独总比滥情好 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:23

    This is what worked for me if you want to run a simple command instead of giving a seperate file

    import subprocess
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    process.wait()
    print process.returncode
    

    To get returncode of process you can use process.returncode To get response you can use process.communicate()

    in case if you are confuse you can just test this code by using command="ls"

    if you are getting returncode other than 0 then you can check here what that error code means: http://tldp.org/LDP/abs/html/exitcodes.html

    For more details about Subprocess: http://docs.python.org/library/subprocess.html

提交回复
热议问题