Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a program with cmd.exe from Python?

I am using python 3.3 and in my code I need something to open cmd.exe with the following arguments and run it. The desired line in cmd.exe should be:

C:\Program Files (x86)\GlobalMapper15>global_mapper.exe script.gms variable

I saw different answers but the most I managed with subprocess.call is to open either cmd.exe, or global_mapper.exe. I did not manage to obtain the line above in cmd.exe.

I tried so far:

#import subprocess
import os
os.system("cmd.exe C:\Program Files (x86)\GlobalMapper15\global_mapper.exe script.gms")
#subprocess.call(["cmd.exe C:\Program Files (x86)\GlobalMapper15\global_mapper.exe", "script.gms"])

Neither of them worked well.

Of course, it would be great if the line would be also executed. Can anyone help me make this happen?

Thank you all,

like image 254
Robert Avatar asked Oct 30 '25 19:10

Robert


2 Answers

To run global_mapper.exe in given directory:

#!/usr/bin/env python
from subprocess import call

dir = r"C:\Program Files (x86)\GlobalMapper15"
cmdline = "global_mapper.exe script.gms variable"
rc = call(cmdline, cwd=dir) # run `cmdline` in `dir`

If you want to start the command in a new console window:

rc = call("start cmd /K " + cmdline, cwd=dir, shell=True)
like image 94
jfs Avatar answered Nov 01 '25 11:11

jfs


maybe with this:

import os
os.system("program.exe -parameter")
like image 36
Eduardo Pino Avatar answered Nov 01 '25 11:11

Eduardo Pino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!