[GRASS-user] Using grass.run_commmand

Hi there
I’m using grass.run_command and I have one question: besides setting flags, parameters can I also get error and output from that function like
p=grass.run_command(“r.out.xyz”, parameters=X, flags=“f…”, stdout=output, stderr=error)
Is this possible?
Thanks
Luisa

Luisa Peña wrote:

I'm using grass.run_command and I have one question: besides setting flags,
parameters can I also get error and output from that function like
p=grass.run_command("r.out.xyz", parameters=X, flags="f...", stdout=output,
stderr=error)

Use read_command or pipe_command.

read_command returns the command's output (stdout) as a string.

pipe_command returns the Popen object; read from its .stdout member.
Call the .wait() method when you're done with it. For details of Popen
objects, see:

http://docs.python.org/library/subprocess.html#popen-objects

The most general function is start_command, upon which all of the
*_command functions (except for exec_command) are built. Again, this
function returns the Popen object.

Refer to the source code ($GISBASE/etc/python/grass/script/core.py)
for more details. Most of those functions are only a few lines long.

--
Glynn Clements <glynn@gclements.plus.com>

Hi
Just one question:
is there any difference, when it comes to processing, between run_command and read_command?
and from read_command can I retrieve the error also?
THanks

2011/5/19 Glynn Clements <glynn@gclements.plus.com>

Luisa Peña wrote:

I’m using grass.run_command and I have one question: besides setting flags,
parameters can I also get error and output from that function like
p=grass.run_command(“r.out.xyz”, parameters=X, flags=“f…”, stdout=output,
stderr=error)

Use read_command or pipe_command.

read_command returns the command’s output (stdout) as a string.

pipe_command returns the Popen object; read from its .stdout member.
Call the .wait() method when you’re done with it. For details of Popen
objects, see:

http://docs.python.org/library/subprocess.html#popen-objects

The most general function is start_command, upon which all of the
*_command functions (except for exec_command) are built. Again, this
function returns the Popen object.

Refer to the source code ($GISBASE/etc/python/grass/script/core.py)
for more details. Most of those functions are only a few lines long.


Glynn Clements <glynn@gclements.plus.com>

Luisa Peña wrote:

is there any difference, when it comes to processing, between run_command
and read_command?

run_command() executes the command and waits for it to terminate; it
doesn't redirect any of the standard streams.

read_command() executes the command with stdout redirected to a pipe,
and reads everything written to it. Once the command terminates, it
returns the data written to stdout as a string.

and from read_command can I retrieve the error also?

None of the existing *_command functions redirect stderr. You can do
so with e.g.:

def read2_command(*args, **kwargs):
    kwargs['stdout'] = grass.PIPE
    kwargs['stderr'] = grass.PIPE
    ps = grass.start_command(*args, **kwargs)
    return ps.communicate()

This behaves like read_command() except that it returns a tuple of
(stdout,stderr) rather than just stdout.

--
Glynn Clements <glynn@gclements.plus.com>