Iterate a script that calls to an .exe for variables?

523
3
Jump to solution
06-11-2014 02:09 PM
TomKearns
Occasional Contributor II
I have a tool that I would like to run on each file in a folder.  The tool runs in arcmap and is a python shell that concocts a command line command that then calls to an .exe. 

Is there a way to hard code within the python script variables the .exe is looking for?  There are 7 variables, they will all remain the same aside from the first, which will just change for each FeatureClass.

Some of the code is as follows:


[HTML]### maybe an output file name was selected
if sys.argv[5] != "#":
    command.append("-o")
    command.append('"'+sys.argv[5]+'"')

### maybe an output directory was selected
if sys.argv[6] != "#":
    command.append("-odir")
    command.append('"'+sys.argv[6]+'"')

### maybe an output appendix was selected
if sys.argv[7] != "#":
    command.append("-odix")
    command.append('"'+sys.argv[7]+'"')[/HTML]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III
You could either place the command arguments in your script as constants or pass them in via tool parameters.

# Option 1 - Get feature class from tool parameter arg1 = arcpy.GetParameterAsText(0)  # Define static parameters ARG2 = "ABC" ARG3 = "123" ARG4 = "456" ARG5 = "#" ARG6 = "XYZ" ARG7 = "#"  # Process parameters into command line if ARG2 != "#":     command.append("-o")     command.append('"'+ARG2+'"')



Note - there is no particular language construct to declare a constant in python, it is just a variable. They are typically declared as variables with uppercase names.

# Option 2 - Get all variables from tool parameters arg1 = arcpy.GetParameterAsText(0) arg2 = arcpy.GetParameterAsText(1) arg3 = arcpy.GetParameterAsText(2) arg4 = arcpy.GetParameterAsText(3) arg5 = arcpy.GetParameterAsText(4) arg6 = arcpy.GetParameterAsText(5) arg7 = arcpy.GetParameterAsText(6)  # Process parameters into command line if arg2 != "#":     command.append("-o")     command.append('"'+arg2+'"')


The second option allows you to change the parameters without editing the script. However, you will need to create all of the parameters in the tool properties and give them default values.

If they are constant values then I would probably go with option 1.

Owen
www.spatialxp.com.au

View solution in original post

0 Kudos
3 Replies
OwenEarley
Occasional Contributor III
You could either place the command arguments in your script as constants or pass them in via tool parameters.

# Option 1 - Get feature class from tool parameter arg1 = arcpy.GetParameterAsText(0)  # Define static parameters ARG2 = "ABC" ARG3 = "123" ARG4 = "456" ARG5 = "#" ARG6 = "XYZ" ARG7 = "#"  # Process parameters into command line if ARG2 != "#":     command.append("-o")     command.append('"'+ARG2+'"')



Note - there is no particular language construct to declare a constant in python, it is just a variable. They are typically declared as variables with uppercase names.

# Option 2 - Get all variables from tool parameters arg1 = arcpy.GetParameterAsText(0) arg2 = arcpy.GetParameterAsText(1) arg3 = arcpy.GetParameterAsText(2) arg4 = arcpy.GetParameterAsText(3) arg5 = arcpy.GetParameterAsText(4) arg6 = arcpy.GetParameterAsText(5) arg7 = arcpy.GetParameterAsText(6)  # Process parameters into command line if arg2 != "#":     command.append("-o")     command.append('"'+arg2+'"')


The second option allows you to change the parameters without editing the script. However, you will need to create all of the parameters in the tool properties and give them default values.

If they are constant values then I would probably go with option 1.

Owen
www.spatialxp.com.au
0 Kudos
TomKearns
Occasional Contributor II
So setting the parameters as constants would work even thought the .exe will be looking for them to be defined? 

Ideally the code would break down like this:

set parameters (except name of featureclass) as constants
start iteration within folder on featureclasses
     Run exe with new name but same parameters.
     Save to different folder.
     move to next featureclass




I just wasn't sure, since the .exe is looking for 7 inputs, if it would work.   There is nothing coded which would pass the constants to the .exe.  I felt like it would still be looking for inputs.



Now having thought it through, it makes sense since sys.argv is coming from the script, not the exe. 

Thanks
0 Kudos
OwenEarley
Occasional Contributor III
sys.argv[] is the list of variables being passed to the python script - with sys.argv[0] being the first argument. The arcpy library has alternate ways of accessing these same variables such as arcpy.GetParameter(0) and arcpy.GetParameterAsText(0).

These variables can be different to the command line arguments that you are passing to the exe.

It appears that you are basically building up a command string including the arguments that your exe requires.

If the only thing that changes is the feature class parameter you could build the command line with arguments using the string format function.

# define you exe path
exe_path = r"C:\temp\YourApp.exe"

# start iteration within folder on featureclasses
for fc in folder:
    cmd = '"{0}" "{1}" "arg2" "arg3" "arg4" "arg5" "arg6" "arg7"'.format(exe_path, fc)
    # run your cmd


Just change the arguments 2-7 with your constant values.

Hope this helps.

Owen
www.spatialxp.com.au
0 Kudos