Run external program

3277
3
Jump to solution
09-04-2015 05:03 AM
PSArcOnlinePSArcOnline
New Contributor III

Hello,

I try to run a external program from within arcmap.

Wherefore I made a model with the model builder. So I can select a point in my shape, run my model and the external program should run with a variable from this point. To start the program I use a simple python script.

To explain my problem, I have included the following line of code with which I started. If I uses this, the script does nothing, no error and no execution of anything:

os.system(r'C:\Program Files (x86)\AquaInfo\AI-Viewer.exe SystemMDW=C:\Program Files (x86)\AquaInfo\Aqua.mdw|UserMDB=C:\Program Files (x86)\AquaInfo\System\AI_mbn.mdb|DataBaseName=C:\Users\ps\Desktop\atest\Test.mdb|UserId=99999998|ObjIdsList=7297|SelObjId=7297|ObjType= 0|')

If I put this into windows cmd the program starts:

"C:\Program Files (x86)\AquaInfo\AI-Viewer.exe" "SystemMDW=C:\Program Files (x86)\AquaInfo\Aqua.mdw|UserMDB=C:\Program Files (x86)\AquaInfo\System\AI_mbn.mdb|DataBaseName=C:\Users\ps\Desktop\atest\Test.mdb|UserId=99999998|ObjIdsList=7297|SelObjId=7297|ObjType= 0|"

If I am using this python code in my module, the program starts with an error because obviously some variables are not set:

os.system(r'"C:\Program Files (x86)\AquaInfo\AI-Viewer.exe"')

Same if I try this:

os.system(r'"C:\Program Files (x86)\AquaInfo\AI-Viewer.exe"' + " SystemMDW=C:\Program Files (x86)\AquaInfo\Aqua.mdw") 

As soon as I enter the Pipe Character "|" the script doesn't work any more. I tried to replace "|" with chr(124) but still the same error.

os.system(r'"C:\Program Files (x86)\AquaInfo\AI-Viewer.exe"' + " SystemMDW=C:\Program Files (x86)\AquaInfo\Aqua.mdw|")

I am not very firm with python, I am more experienced in vbs. Is there a problem with the "|" Character in Python Strings then it is used for a path with os.system?

How can I solve this.

Any ideas?

Thanks for your help.

0 Kudos
1 Solution

Accepted Solutions
ShaunWalbridge
Esri Regular Contributor

The reason that the pipe character is special, is on UNIX based operating systems, it's used to denote steps in a pipeline, and is handled specially. In your case this isn't what you want, I'd recommend using the subprocess module instead of os.system:

import subprocess
subprocess.call([r'C:\Program Files (x86)\AquaInfo\AI-Viewer.exe',
                 r'SystemMDW=C:\Program Files (x86)\AquaInfo\Aqua.mdw|UserMDB=C:\Program Files (x86)\AquaInfo\System\AI_mbn.mdb|DataBaseName=C:\Users\ps\Desktop\atest\Test.mdb|UserId=99999998|ObjIdsList=7297|SelObjId=7297|ObjType= 0|'])

I don't have the AquaInfo software to test against, but in an example script, that passed the arguments correctly.

Cheers,

Shaun

View solution in original post

3 Replies
ShaunWalbridge
Esri Regular Contributor

The reason that the pipe character is special, is on UNIX based operating systems, it's used to denote steps in a pipeline, and is handled specially. In your case this isn't what you want, I'd recommend using the subprocess module instead of os.system:

import subprocess
subprocess.call([r'C:\Program Files (x86)\AquaInfo\AI-Viewer.exe',
                 r'SystemMDW=C:\Program Files (x86)\AquaInfo\Aqua.mdw|UserMDB=C:\Program Files (x86)\AquaInfo\System\AI_mbn.mdb|DataBaseName=C:\Users\ps\Desktop\atest\Test.mdb|UserId=99999998|ObjIdsList=7297|SelObjId=7297|ObjType= 0|'])

I don't have the AquaInfo software to test against, but in an example script, that passed the arguments correctly.

Cheers,

Shaun

RhettZufelt
MVP Frequent Contributor

I had something "similar", trying to pass values to the external gpg.exe program.

Here is how I resolved it:

TheCommand = "gpg.exe --homedir ./Dependencies --passphrase MyPassword --output C:/crashdata/" + fname.replace('.ASC','.CSV') +" --decrypt " + fname
           
os.system(TheCommand)

Basically, I keep trying the string concatenation in IDLE (or your favorite interpreter) until the string looks exactly like I need it (all pipes, tags, etc.) then os.system on the variable containing the string.

R_

I see Shaun posted while I was typing.  I think the subprocess.call would be the "proper" way, but with my gpg.exe one, I could not get the subprocess to work, no matter how I tried.

PSArcOnlinePSArcOnline
New Contributor III

Thank you both for the help.

subprocess did the trick.

And thanks for the "Pipe" explanation.

0 Kudos