S M ANANTHARAMAN
S M ANANTHARAMAN

Reputation: 67

Execute windows command from python3.4

I have a command which I use for deployment from windows command line. Now I need to run the same from an external python3.4 script.

The command is C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild "D:\WebService\WebService.sln" /p:DeployOnBuild=true /p:PublishProfile="D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml" /p:AllowUntrustedCertificate=true /p:UserName=name /p:Password=PASSWORD.

How can I achieve this. I tried subprocess . But it's not working.Please help me out.

Upvotes: 1

Views: 152

Answers (2)

cdarke
cdarke

Reputation: 44344

Your problems appear to be the \ and " characters, so use raw strings. Also, it is safer to use a list:

proc = subprocess.Popen(
            [r"C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild",
             r"D:\WebService\WebService.sln",
             r"/p:DeployOnBuild=true",
             r"/p:PublishProfile=D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml",
             r"/p:AllowUntrustedCertificate=true",
             r"/p:UserName=name",
             r"/p:Password=PASSWORD"])

proc.wait()

Strictly speaking you don't need raw strings for all those parameters, but it is safer to do so with Windows paths. You only need the internal double quotes if you have embedded whitespace (as the first parameter). Here we are not using a shell, set shell=True as a parameter if you need one. A reason to use a shell on Windows is for filename association, but you don't appear to be using that here.

Upvotes: 3

Rick Sullivan
Rick Sullivan

Reputation: 188

Can you post some code with what you have tried so far?

The subprocess module should be able to handle that, with something like

theproc = subprocess.Popen(["COMMAND HERE"])
theproc.communicate()

or you could try with the shell flag

theproc = subprocess.Popen(["COMMAND HERE"], shell=True)

Upvotes: 0

Related Questions