On Jul 15, 2008, at 10:42 PM, <grass-dev-request@lists.osgeo.org> <grass-dev-request@lists.osgeo.org > wrote:
Message: 2
Date: Wed, 16 Jul 2008 04:14:44 +0100
From: Glynn Clements <glynn@gclements.plus.com>
Subject: Re: [GRASS-dev] Python Scripting
To: "W. Chris Carleton" <w_chris_carleton@hotmail.com>
Cc: grass-dev@lists.osgeo.org
Message-ID: <18557.26532.411242.947615@cerise.gclements.plus.com>
Content-Type: text/plain; charset=us-asciiW. 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=iThe 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@gclements.plus.com>