1

I think I just need a second set of eyes on this. I'm just using Python to capture some user inputs to pass down to GME command. I'm only using the export to CSV to hammer out the behavior, but the final outcome will be an ArcPY tool for creating sample points. Fun stuff. (Please ignore my imported modules).

    import arcpy, os, sys, subprocess

inFeatures = r"D:\TEMP\junk.shp"
OutputLocation = r"D:\TEMP"
OutputBasename = os.path.splitext(inFeatures)[0]
OutputCSV = OutputBasename + ".csv"
GME = r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe'
ARG = ' export.csv(in=\"' + inFeatures + '", out=\"' + OutputCSV + '");'
CMD = GME + ARG
print CMD
subprocess.call(CMD)

The printed command is as follows:

C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe export.csv(in="D:\TEMP\junk.shp", out="D:\TEMP\junk.csv");

And that looks to be correct

I have excluded the '-c' switch for the sake of debugging.

When GME is launched, the following output is provided.

Found R here: C:\Program Files\R\R-3.0.1\bin To switch to a different version of R use the 'Set R executable folder' item in the File menu, or use the r.setpath() command. export.csv(in=D:\TEMP\junk.shp, out=D:\TEMP\junk.csv); Error: Variable name could not be resolved (D:\TEMP\junk.shp) or quotes were omitted when providing a text parameter; command cannot be interpreted

The actual command within GME should look like this:

export.csv(in="D:\TEMP\junk.shp", out="D:\TEMP\junk.csv");

So, for some reason my quotes are not getting passed to GME?

The only clue I have related to this in the documentation is as follows:

Please note that you need to add a backslash before all quote marks in the commands text, otherwise it is the same syntax as GME. For instance, here is a Python call to GME that tells it to run the GME script called "E:ndatangmescript.txt": subp.call(r'C:nProgram FilesnSpatialEcologynGMEnSEGME.exe -c run(in=n"E:ndatangmescript.txtn");');

Also, please note I'm still learning the ins & outs of subprocess.

Aside from 'is my syntax correct?', I'm not sure how to roll this into a more direct question.

Thanks! Z

EDIT: I'm adding the only Python reference in the help document:

You must rst load the subprocess Python library (using: import subprocess as subp) at the beginning of your Python session. To call GME from the command line you use the following generic syntax: os.system(r'pathnSEGME.exe commands') where path is the full directory path to the SEGME command, usually: subp.call(r'C:nProgram FilesnSpatialEcologynGMEnSEGME.exe commands') and commands is the list of commands you wish to execute. Note that the r at the beginning of that path name is not a mistake: it tells Python to interpret the following text literally. Please note that you need to add a backslash before all quote marks in the commands text, otherwise it is the same syntax as GME. For instance, here is a Python call to GME that 17 tells it to run the GME script called "E:\data\gmescript.txt": subp.call(r'C:\Program Files\SpatialEcology\GME\SEGME.exe -c run(in=\"E:\datangmescript.txt\");'); (This command has been displayed on two lines here because it is too long for a single line, though in Python it would be a single line). Note that by including -c directly after SEGME.exe, this will force the GME interface to close when it has nished running the specied commands.

  • You might find it easier to concatenate your strings and variables if you use str.format(). ARG = ' export.csv(in="{0}", out="{1}");'.format(inFeatures, OutputCSV) – Paul Sep 3 '13 at 14:39
  • Hmm, Well Paul, no dice, it's still passing the same input up to GME, with the identical error, no quotes making it there. I even tried: ARG = ' export.csv(in='"{0}"', out='"{1}"');'.format(inFeatures, OutputCSV) – Clickinaway Sep 11 '13 at 19:47
  • Paul, have a look at the added documentation above, looks like i may need to escape a double quote as well? in=\E:\data\something.txt\" – Clickinaway Sep 11 '13 at 20:10
0

Maybe this will help: Go to http://docs.python.org/2/library/subprocess.html

Scroll right down to to "17.1.5.1. Converting an argument sequence to a string on Windows"

  • User, I'm just circling back to this and if you look at the code, I believe I am indeed adding the required back slash preceding a double quote. I'm not sure what else you might be referring to? – Clickinaway Sep 11 '13 at 19:25
0

Ok, so for lack of a better term, you do indeed need to 'escape' your initial \ Of course, this get's fun with Py Syntax and "" and all of that. I'm going to post the code below because this does work. That said, I'm open to any and all suggestions on how to improve it, I'm not advertising this as elegant!

import os, subprocess

inFeatures = r"D:\TEMP\junk.shp"
Input = "\\" + '"'  + inFeatures + "\\" + '"'
OutputLocation = r"D:\TEMP"
OutputBasename = os.path.splitext(inFeatures)[0]
OutputCSV = "\\" + '"' + OutputBasename + ".csv" + "\\" 
GME = r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe'
ARG = " export.csv(in=" + Input + ", out=" + OutputCSV + '");'
CMD = GME + ARG
print CMD
subprocess.call(CMD)

The actual command passed to GME looks like this:

C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe export.csv(in=\"D:\TEMP\junk.shp\", out=\"D:\TEMP\junk.csv\");

Export CSV was simply my pathway to figure out this process. My real goal is leveraging the various sampling point tools and I intend to wrap this all up as a Py script for Arc ToolBox. If anyone is interested I can display that when I get it done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.