[GRASS-dev] Python Scripting

Michael Barton michael.barton at asu.edu
Wed Jul 16 02:06:33 EDT 2008


On Jul 15, 2008, at 10:42 PM, <grass-dev-request at lists.osgeo.org> <grass-dev-request at lists.osgeo.org 
 > wrote:

> Message: 2
> Date: Wed, 16 Jul 2008 04:14:44 +0100
> From: Glynn Clements <glynn at gclements.plus.com>
> Subject: Re: [GRASS-dev] Python Scripting
> To: "W. Chris Carleton" <w_chris_carleton at hotmail.com>
> Cc: grass-dev at lists.osgeo.org
> Message-ID: <18557.26532.411242.947615 at cerise.gclements.plus.com>
> Content-Type: text/plain; charset=us-ascii
>
>
> W. Chris Carleton wrote:
>
>> I'm trying to automate some modules in GRASS 6.3.2 using Python  
>> scripts
>> and g.parser. I've had a look at the examples in /Scripts, but I'm
>> having some trouble. I want to pass arguments to a GRASS module by
>> iterating over a list of values. Here's what I have;
>
>> 	v.extract input="$GIS_OPT_INPUT" output="$GIS_OPT_OUTPUT""_"i \
>> type="point" layer=1 new=-1 list=i
>
> The above is shell syntax.
>
> In Python, it would look something like:
>
> 	import subprocess
>
> 	...
>
> 	subprocess.call([
> 		"v.extract",
> 		"input=%s" % os.getenv("GIS_OPT_INPUT"),
> 		"output=%s_%s" % (os.getenv("GIS_OPT_OUTPUT"), i),
> 		"type=point", "layer=1", "new=-1", "list=%s" % i])

Glynn,

How do subprocess.call and subprocess.Popen compare for running GRASS  
commands from inside Python scripts? Is call easier than Popen in this  
context?

Michael

>
>
> The main differences between Python and the shell:
>
> 1. All strings must be quoted.
>
> 2. Environment variables have to be accessed via os.getenv().
>
> 3. String manipulation requires explicit operators, e.g. +
> (concatenation) or % (substitution).
>
> 4. Executing external programs requires the functions in the
> subprocess module.
>
> -- 
> Glynn Clements <glynn at gclements.plus.com>



More information about the grass-dev mailing list