Select to view content in your preferred language

Calling Other Programs into Python for Processing?

3836
14
06-23-2011 08:57 AM
MikeMacRae
Frequent Contributor
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
MikeMacRae
Frequent Contributor
Actually, the os.startfile() runs pretty cool. It might be more of what I am looking to do. I'm thinking it might take a little more than my abilities to make this program run in the background the way I would like.

My thought process now is maybe just to prompt the script to run like this:

1. Run my python script and let it process my maps, layers, etc
2. When the script gets to the block to os.startfile(my.rpl program), the .rpl opens and in the same breath, pauses the python program
3. allow user to enter the information in the .rpl program and run it.
4. when they close the .rpl program, the rest of the python script continues on

I see some syntaxes like os.wait() that may pause the python script, maybe something like os.when_program_closes_continue_python....just reaching at straws now 🙂
0 Kudos
LoganPugh
Frequent Contributor
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 in with values .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.


That sounds correct but I have not heard of this .rpl program and couldn't say how it should be called on the command line. But to me it still sounds like the .rpl is a file that is run by some other executable you have not specified as of yet.

Actually, the os.startfile() runs pretty cool. It might be more of what I am looking to do. I'm thinking it might take a little more than my abilities to make this program run in the background the way I would like.

My thought process now is maybe just to prompt the script to run like this:

1. Run my python script and let it process my maps, layers, etc
2. When the script gets to the block to os.startfile(my.rpl program), the .rpl opens and in the same breath, pauses the python program
3. allow user to enter the information in the .rpl program and run it.
4. when they close the .rpl program, the rest of the python script continues on

I see some syntaxes like os.wait() that may pause the python script, maybe something like os.when_program_closes_continue_python....just reaching at straws now 🙂


Using subprocess.call() should pause the script until the called process terminates.
0 Kudos
MikeMacRae
Frequent Contributor
That sounds correct but I have not heard of this .rpl program and couldn't say how it should be called on the command line. But to me it still sounds like the .rpl is a file that is run by some other executable you have not specified as of yet.



Using subprocess.call() should pause the script until the called process terminates.


Wow, this is a sharp learning curve. Logan, the .rpl program was copied to a folder on my network drive. I've always accessed it from there, but the folder it sits in did not include the executable. I didn't know where to find it so I re-downloaded the program from the internet which gave me all the associated files (including the executable)....lesson learned. I set up the subroutine as follows:

subprocess.call(['C:\Downloads\Multiple File Search Replace\REPLACE.exe','C:\Downloads\Multiple File Search Replace\Noname.rpl'])


I threw it into my existing python script and it runs like a thing of beauty. It calls the .rpl program (and pauses the rest of my python script), allows me to monkey around with it and when I close the program, the rest of the python runs.

Much thanks for everyones help. I have no doubt this will open a can of new questions at some point!

Cheers,
Mike
0 Kudos
LoganPugh
Frequent Contributor
Cool, glad you got it working. I think the next logical step would be doing the XML modifications directly in Python -- for this you might take a look at xml.etree.ElementTree (built-in) or lxml.etree (3rd party).
0 Kudos
MikeMacRae
Frequent Contributor
Thanks again Logan. I actually stumbled across this module in my tour of the internet today. It has some functions and classes that may eventually replace the subroutine and .rpl program.
0 Kudos