[GRASS-user] Silent commands in python

Glynn Clements glynn at gclements.plus.com
Tue Dec 4 02:18:58 EST 2007


Daniel Victoria wrote:

> This might be more of a python problem but here it goes...
> I wrote a water balance script in python to run under GRASS. What I
> would like now is that the commands in the python script would be
> silent, that is, no return from stdout or stderr.
> For instance every time I call a r.mapcalc function I get the 0-100%
> count. I'd like to silence that and other commands.
> 
> In the shell I would silence command outputs using &> /dev/null, for exemple:
> echo "blabla=2*bla" | r.mapcalc &> /dev/null
> 
> But in python that does not work using the os.system() function, that is:
> os.system("echo 'blabla=2*bla' | r.mapcalc &> /dev/null")
> 
> Anyone has any tips on the matter?

Don't use os.system(); use subprocess.Popen() instead. E.g.:

	import os
	import subprocess
	...
	proc = subprocess.Popen(["r.mapcalc", "blabla=2*bla"], stdout=file(os.devnull, "w"))
	retcode = proc.wait()

This avoids a number of pitfalls with using a shell, including the
fact that shell syntax varies widely between platforms.

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


More information about the grass-user mailing list