[英]Running Subprocess from Python
I want to run a cmd exe using a python script. 我想使用python脚本运行cmd exe。
I have the following code: 我有以下代码:
def run_command(command):
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return p.communicate()
then i use: run_command(r"C:\\Users\\user\\Desktop\\application\\uploader.exe") this returns the option menu where i need to specify additional parameter for the cmd exe to run. 然后我使用:run_command(r“ C:\\ Users \\ user \\ Desktop \\ application \\ uploader.exe”)这将返回选项菜单,在这里我需要为cmd exe指定附加参数才能运行。 So i pass additional parameters for the cmd exe to run. 因此,我为cmd exe传递了其他参数来运行。 How do i accomplish this. 我该如何做到这一点。 I've looked at subprocess.communicate but i was unable to understand it 我看了subprocess.communicate,但我听不懂
If uploader.exe
accepts command line options, then you could try subprocess.call
in the following manner: 如果uploader.exe
接受命令行选项,则可以按以下方式尝试subprocess.call
:
If your command is uploader.exe
, the directory of uploader is C:\\Users\\...\\application
, and the additional parameter is x
, you could try 如果您的命令是uploader.exe
,则上uploader.exe
的目录是C:\\Users\\...\\application
,附加参数是x
,您可以尝试
import subprocess
def run_command(command, directory, arg):
return subprocess.call(["command %s"%arg], cwd=directory, shell=True)
run_command("uploader.exe", "C:\\Users\\..\\application", "x")
However, this assumes you do not need to actually interact with indexer.exe
after you Popen
to it. 然而,这是假定你并不需要真正互动indexer.exe
你以后Popen
它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.