ssbsts
ssbsts

Reputation: 844

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. 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.

Upvotes: 0

Views: 55

Answers (1)

MattDMo
MattDMo

Reputation: 102902

subprocess.Popen() can take a list of arguments as well as a string. 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. 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.

Upvotes: 2

Related Questions