Launch a shell command with in a python script, wait for the termination and return to the script

后端 未结 7 662
失恋的感觉
失恋的感觉 2020-11-28 22:09

I\'ve a python script that has to launch a shell command for every file in a dir:

import os

files = os.listdir(\".\")
for f in files:
    os.execlp(\"myscri         


        
7条回答
  •  再見小時候
    2020-11-28 22:57

    You can use subprocess.Popen. There's a few ways to do it:

    import subprocess
    cmd = ['/run/myscript', '--arg', 'value']
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    for line in p.stdout:
        print line
    p.wait()
    print p.returncode
    

    Or, if you don't care what the external program actually does:

    cmd = ['/run/myscript', '--arg', 'value']
    subprocess.Popen(cmd).wait()
    

提交回复
热议问题