process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=
    subprocess.PIPE)
for line in process.stdout:
    result.append(line)
errcode = process.returncode
for line in result:
    print(line)
if errcode is not None:
    raise Exception('cmd %s failed, see above for details', cmd)
Running windows shell commands with python