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

后端 未结 7 685
失恋的感觉
失恋的感觉 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 23:05

    subprocess: The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

    http://docs.python.org/library/subprocess.html

    Usage:

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

提交回复
热议问题