[GRASS-user] Running an external executable file from a Python Script

Ricardo Filipe Soares Garcia da ricardo.garcia.silva at gmail.com
Thu Mar 10 10:59:23 EST 2011

Hi (Olá)

in order to figure out what is your current working folder (or active
folder) you can do

# python code

import os

# end of code

This will return a string with your current working folder.
As Glyn is stating, if you are going to call this external binary from
within a python script you can use a grass.Popen object (or just a
normal subprocess.Popen).
The grass.Popen object allows you to capture your external binary's
output and (eventual) error messages.

In the following example, I'm running the 'ls -l' external command,
using the /home/Documents directory as a working directory for the
external command. Please adapt to your problem / operating system:

# python code

import grass.script as grass

externalCommand = ["ls", "-l"] # note that it is a list
externalProcess = grass.Popen(externalCommand, stdout=grass.PIPE,
stderr=grass.PIPE, cwd="/home/ricardo/Documents")
sdtout, stderr = externalProcess.communicate()

# to show the output of your external program

# end of code

Hope it helps ;)

2011/3/10 Glynn Clements <glynn at gclements.plus.com>:
> António Rocha wrote:
>> My question is, when I'm running a GRASS python Script what is my active
>> folder in order to place there my Parameter file? Or, is there any way
>> to change my "active folder" while I'm running GRASS python Script?
> By "active folder", I presume that you're referring to the current
> directory (aka working directory, current working directory or CWD).
> This is inherited from the calling process; e.g. if you run a script
> from a shell, the script's current directory will be the shell's
> current directory.
> When executing a command via subprocess.Popen(), you can specify its
> current directory via the cwd= parameter. The grass.Popen() and
> grass.call() functions accept this parameter, as do all of the
> grass.*_command() functions for running GRASS modules.
> You can change the current directory for the current process using
> os.chdir(), but that should normally be avoided, as any relative
> filenames will then be interpreted relative to the new current
> directory, whereas the user probably intended them to be relative to
> the initial current directory.
> --
> Glynn Clements <glynn at gclements.plus.com>
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user

___________________________ ___ __
Ricardo Garcia Silva

More information about the grass-user mailing list