Calling Other Programs into Python for Processing?

2948
14
06-23-2011 08:57 AM
MikeMacRae
Occasional Contributor III
Hey everyone,

I am at a stage with Python where I would like to explore using it with other applications. In my case, I am automating a number of maps and editing layers and feature classes in the process. Before I started doing this stuff through python, I had a number of manual processes. One of which is to use an .rpl program (find and replace program) when updating metadata .xml's in order to import them into my feature class metadata.

My question is, can I set a code block in python, to call the .rpl program (or any other program for that matter) to do some processing while my script runs? I hope this makes sense....

I'd like my whole python script basically do this:

1. Make some changes in my .mxd
2. Make some changes in some feature classes
3. Update my xml files by calling in the .rpl file to do this for me

Cheers,
Mike
Tags (2)
0 Kudos
14 Replies
JasonScheirer
Occasional Contributor III
You can use subprocess.

import subprocess
subprocess.call(['rplrunner.exe', r'c:\path\to\my.rpl'])
0 Kudos
MikeMacRae
Occasional Contributor III
Thanks for the response Jason. This will rock my python world if I can get it to work.

I used the subprocess command you posted and implemented my own file and path. I am getting an error using traceback.

import subprocess

subprocess.call(['RunMetadata.exe', r'Z:\ESRI\IOR_Run_Metadata_2009\RunMetadata.rpl'])



Error is:

PYTHON ERRORS:
Traceback info:
  File "Z:/ESRI/Python/Test Scripts/SubProcess.py", line 9, in <module>
    subprocess.call(['RunMetadata.exe', r'Z:\ESRI\IOR_Run_Metadata_2009\RunMetadata.rpl'])

Error Info:
[Error 2] The system cannot find the file specified

ArcPy ERRORS:


Any reason why it can't find the specified file? I tried replacing .exe with .rpl, but the same erro comes up.
0 Kudos
LoganPugh
Occasional Contributor III
Either fully qualify the path to the executable you want to run, put it in your Windows PATH environment variable, or change the current working directory in Python to the directory containing the executable using os.chdir(). The first method is probably the easiest.
0 Kudos
MikeMacRae
Occasional Contributor III
Thanks Logan. I have fully qualified the path like this:

subprocess.call(['Z:\ESRI\IOR_Run_Metadata_2009\RunMetadata.rpl', r'Z:\ESRI\IOR_Run_Metadata_2009\RunMetadata.rpl'])


The error I am getting now is:


PYTHON ERRORS:
Traceback info:
  File "Z:/ESRI/Python/Test Scripts/SubProcess.py", line 9, in <module>
    subprocess.call([r'Z:\ESRI\IOR_Run_Metadata_2009\RunMetadata.rpl', r'Z:\ESRI\IOR_Run_Metadata_2009\RunMetadata.exe'])

Error Info:
[Error 193] %1 is not a valid Win32 application

ArcPy ERRORS:


Does this mean that win32 will not recognize the application? Is there a way to make it do this?
0 Kudos
JasonScheirer
Occasional Contributor III
Switch the two arguments, put the .exe first.
0 Kudos
MikeMacRae
Occasional Contributor III
Oops, looks like I posted the second error I got with the first script I tried. I've tried a combination of .exe and .rpl (ie make both paths contain .exe/.rpl or a combination of both) I seem to be getting this same error with every attempt.
0 Kudos
MikeMacRae
Occasional Contributor III
I'm actually trying to understand the arguments. The syntax on docs.python is:

subprocess.call(*popenargs, **kwargs)

The arguments are:

args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument. When executable is given, the first item in the args sequence is still treated by most programs as the command name, which can then be different from the actual executable name



I'm not sure what it means by this bolded section. In the location of the rpl file, there aren't any other files. There is no .exe file (I've made hidden files and folders visible) Is the .rpl file considered an executable as well? Should it recognize the .exe extension even though it doesn't seem to have one? I apologize if the .rpl file isn't something you all are aware of....
0 Kudos
LoganPugh
Occasional Contributor III
What is the executable associated with .rpl files? That is what you should be calling as your first argument, and the .rpl file your second argument. Alternatively, use os.startfile() to run the .rpl file using the default associated program (akin to double-clicking the file in Windows).
0 Kudos
MikeMacRae
Occasional Contributor III
Sorry, I'll try to reword what it is I am doing. The .rpl is the program itself ( I probably should have mentioned this is the program I am using instead of refering to it as a file). It is an interface that allows me to find and replace tags with values in .xml files which I want to import into feature classes later on. The .rpl is the executable.

I'm thinking that if I interpret what you have said, Logan, is that my first argument should be the location of the program I want to call (or open) and the second argument should be the file or files that I want to run it against (ie the .xml)?


Basically my current process is this:

1. open the .rpl program
2. enter the information that I need to edit (ie user name, dates, project site name, email address, etc...)
3. Run that against 12 template .xml files which finds tags and string variables and replaces them with the information I entered into the .rpl program
4. Import .xml into the metadata to the matching feature class.
5. Fin

I hope this makes it a little more clearer as to my process and end result.
0 Kudos