[GRASS-dev] pyGRASS return g.mlist() output into a python list

Pietro peter.zamb at gmail.com
Mon Sep 9 14:04:17 PDT 2013

Hi Yann,

On Mon, Sep 9, 2013 at 8:14 AM, Yann Chemin <ychemin at gmail.com> wrote:
> so the pattern returns to the CLI not to the output list "list"
> list=g.mlist(type='rast',pattern='interpolation_*') <= not working

it is correct that g.mlist return None, because you are just calling a
GRASS module that write to the stdout and not a python function that
return a python object, therefore you can save the stdout and then
parse it with:

>>> from grass.pygrass.modules.shortcuts import general as g
>>> import subprocess as sub
>>> gl = g.mlist
>>> gl(type='rast', pattern='elev-*', stdout=sub.PIPE)
>>> gl.outputs.stdout.split()
['elev-000-000', 'elev-000-001', 'elev-000-002', 'elev-001-000',
'elev-001-001', 'elev-001-002', 'elev-002-000', 'elev-002-001',

or you can use the "glist" method of the Mapset class

>>> from grass.pygrass.gis import Mapset
>>> m = Mapset()
>>> m.glist('rast', pattern='elev-*')
['elev-002-000',  'elev-000-000',  'elev-000-002',  'elev-000-001',
'elev-001-001',  'elev-002-002',  'elev-002-001',  'elev-001-000',

that use the "G_list" function through ctypes and therefore is faster.
If you choose the first solution is better if you use directly the
function in python.script.core.mlist_grouped as suggested by Anna.

Best regards


More information about the grass-dev mailing list