使用python的subprocess启动windows程序提示WindowsError: [Error 6] The handle is invalid

时间:2023-12-27 14:38:25

代码如下:

subp = subprocess.Popen(cwd_path + "test.exe", cwd = cwd_path, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

执行时,从异常捕获中看到错误 WindowsError: [Error 6] The handle is invalid

最后在 https://bugs.python.org/issue3905 找到了解决方法

Specifying PIPE for all subprocess.Popen handles is the only way I've
found to get around this problem:

p = subprocess.Popen(["python", "-c", "print 32"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

p.stdin.close()

subp = subprocess.Popen(cwd_path + "test.exe", cwd = cwd_path, shell = True, stdin=subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
subp.stdin.close()