3

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,

Robert
  • 521
  • 2
  • 8
  • 14
  • _"the most I managed with subprocess.call is to open either cmd.exe, or global_mapper.exe."_ I would be interested in seeing this code. – Kevin Feb 19 '14 at 13:54
  • @Kevin well, you can test that even by running something as simple as: import subprocess subprocess.call(["cmd.exe"]) This opens cmd.exe. It is not enough though, I still need to add all the extra arguments on the line. – Robert Feb 19 '14 at 14:06
  • maybe this can help you. http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – Eduardo Pino Feb 19 '14 at 14:14
  • What happened when you tried adding all the extra arguments to `subprocess.call`? So far, you haven't posted any code, so it looks like you're just asking us to write your project for you. – Kevin Feb 19 '14 at 14:16
  • @EduardoPino Thank you, I tried to follow that code but I got stuck. I will keep trying. – Robert Feb 19 '14 at 14:39
  • @Kevin I am asking for some help in doing this. I already posted the basic usage that opens one or the other. I tried subprocess.call(["cmd.exe", "C:\Program Files (x86)\GlobalMapper15\global_mapper.exe script.gms"]) but it opens only cmd.exe with the following line: C:\Python33> I am trying to learn it step by stem since I am a newby. I asked because I tried for a few hours and I got stuck, not because I want you to write this for me. Some of the examples out there are too hard to understand for now – Robert Feb 19 '14 at 14:48
  • @Robert: don't post the code in the comments. [Update your question instead](http://stackoverflow.com/posts/21882553/edit). – jfs Feb 19 '14 at 14:50

3 Answers3

8

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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Many thanks for making it clear. I understand now how it works. Geez, programming can really speed things up. Thanks to all, – Robert Feb 19 '14 at 15:19
2

maybe with this:

import os
os.system("program.exe -parameter")
Eduardo Pino
  • 66
  • 1
  • 5
1

you can Create a .bat file and call it

dir = r"C:\Program Files (x86)\GlobalMapper15"
write=''.join((dir,'\\','call.bat'))
f= open(write,'w')
with f:
  f.write('@ECHO ON\n Call global_mapper.exe script.gms variable'
subprocess.call(write,cwd=dir)
salah
  • 106
  • 2
  • 12
  • If you need to do this in Program Files folders, you will likely run into adminstrator permission issues. Otherwise, in a user home or appdata folder, this could work well. – LightCC Aug 04 '22 at 14:31