command execution system python popen and get the return value.

Reprinted from the product is slightly Library  http://www.pinlue.com/article/2020/03/1106/1510011426025.html

Description:

1. communicate with the interactive method, it is possible to directly read data p.stdout not refresh, will not read

2. windows shell parameter indicates whether the windows bat as the execution environment, so only in the implementation of windows system commands such as dir, when the copy must set this parameter to True, the rest of True and False no difference between the results of

3. universal_newlines parameters indicate input and output is in text parsed

4. comnunicate the input parameter is a hand-type input parameters, if the parameter is the command line parameters directly after the program can add

5. no

import subprocess

proc = subprocess.Popen("E:\\__Projects\\c++\\test\\Debug\\test.exe", stdin = subprocess.PIPE,

stdout = subprocess.PIPE, stderr = subprocess.PIPE,universal_newlines=True, shell = False)

straa = "3\n"

strbb = "4\n"

proc.stdin.write(straa)

proc.stdin.write(strbb)

# p.stdin.close()

try:

outs, errs = proc.communicate(timeout=15)

# print(proc.stdout.read())

print(outs)

except TimeoutExpired as e:

proc.kill()

outs, errs = proc.communicate()

 

Published 60 original articles · won praise 58 · Views 140,000 +

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/105392503