[英]Example to call subprocess with cmd.exe
I want/try to run cmd.exe with 2 commands: "ver", then "exit", 我想/尝试使用2个命令运行cmd.exe:“ ver”,然后“ exit”,
from subprocess import Popen, PIPE
def do_process(command, text):
pro = Popen(command, stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = pro.communicate(text.encode())
return out
do_process(['cmd.exe'], 'ver\nexit\n')
it cannot return the output, seems cmd.exe is not ended. 它无法返回输出,似乎cmd.exe没有结束。 What is ok way.
好的方法。
It's not a very reliable method calling cmd.exe
and I'm almost sure you won't be able to communicate with it properly. 调用
cmd.exe
并不是一个非常可靠的方法,而且我几乎可以肯定您将无法与之正确通信。
In case you want to retrieve system version, you might use this: 如果要检索系统版本,可以使用以下命令:
>>> import platform
>>> platform.platform()
'Linux-3.17.4-1-ARCH-x86_64-with-glibc2.2.5'
>>> import platform
>>> platform.platform()
'Windows-7-6.1.7601-SP1'
I made err inside my function, I need to decode
result (I use cp866 on my OS): 我在函数内做了err,我需要
decode
结果(我在操作系统上使用cp866):
def do_process(command, text):
enc = 'cp866'
p = Popen(command, stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = p.communicate(text.encode())
return out.decode(enc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.