如何从python执行命令提示符命令 [英] How to execute a command prompt command from python
本文介绍了如何从python执行命令提示符命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过这样的事情,但没有效果:
command = "cmd.exe"proc = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE)proc.stdin.write("dir c:\\")
解决方案
你可能想尝试这样的事情:
command = "cmd.exe/C dir C:\\"
我不认为你可以输入 cmd.exe
...如果你来自 unix 背景,那么 cmd.exe
有一些丑陋的疣!
根据 Sven Marnach 的说法,您可以通过管道传输到 cmd.exe
.我尝试在 python shell 中执行以下操作:
如您所见,您还有一些工作要做(仅返回第一行),但您或许可以让它发挥作用...
I tried something like this, but with no effect:
command = "cmd.exe"
proc = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
proc.stdin.write("dir c:\\")
解决方案
You probably want to try something like this:
command = "cmd.exe /C dir C:\\"
I don't think you can pipe into cmd.exe
... If you are coming from a unix background, well, cmd.exe
has some ugly warts!
EDIT: According to Sven Marnach, you can pipe to cmd.exe
. I tried following in a python shell:
>>> import subprocess
>>> proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
>>> stdout, stderr = proc.communicate('dir c:\\')
>>> stdout
'Microsoft Windows [Version 6.1.7600]\r\nCopyright (c) 2009 Microsoft Corporatio
n. All rights reserved.\r\n\r\nC:\\Python25>More? '
As you can see, you still have a bit of work to do (only the first line is returned), but you might be able to get this to work...
这篇关于如何从python执行命令提示符命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文