简体   繁体   English

在Python中使用Popen执行带变量的bat脚本

[英]Using Popen in Python to execute bat script with variable

I have a python script that is calling a bat script called testrunner.bat which in turns executes a TestSuite in SOAPUI. 我有一个python脚本,它正在调用名为testrunner.bat的bat脚本,该脚本又在SOAPUI中执行TestSuite。 I actually have gotten the external call to work just fine with the following command: 我实际上已经通过以下命令获得了外部调用的正常工作:

Popen("testrunner.bat -s\"CCT000 - Deploy Software Release\" -R\"TestSuite Report\" -E\"Default environment\" -Ppath.packages.sq=Y:\\NIGHTLY C:\\CI\\HEDeployment\\CI-XXX-DeploySwRelease")

However, I need to be able to have the software "level" to be dynamic and need to pass the variable level into the command in place of "NIGHTLY" so I can specify if it's nightly software, or stable, etc. I have seen that I should break all the arguments up separately, but I am having a hard time. 但是,我需要能够使软件“级别”动态化,并且需要将变量级别传递给命令中的“ NIGHTLY”,以便可以指定它是夜间软件还是稳定软件等。我应该分开分解所有论点,但是我很难过。

subprocess.Popen() can take a list of arguments as well as a string. subprocess.Popen()可以接受参数列表以及字符串。 So, this should work for you: 因此,这应该为您工作:

release_type = "NIGHTLY"
Popen(['testrunner.bat', 
       '-s"CCT000 - Deploy Software Release"', 
       '-R"TestSuite Report"', 
       '-E"Default environment"', 
       '-Ppath.packages.sq=Y:' + release_type, 
       'C:CIHEDeploymentCI-XXX-DeploySwRelease'])

As mentioned in the docs, shlex.split can be very useful for splitting your original command string into pieces. 如文档中所述, shlex.split对于将原始命令字符串拆分成多个部分非常有用。 However, at least in my case, I had to re-add the double quotes. 但是,至少就我而言,我不得不重新添加双引号。

Also, recall that single-quoted strings can contain double quotes, and vice versa, so you don't need to escape the quotes here. 此外,请记住单引号字符串可以包含双引号,反之亦然,因此您无需在此处转义引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM