[GRASS-dev] Python Scripting
Glynn Clements
glynn at gclements.plus.com
Tue Jul 15 23:14:44 EDT 2008
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])
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