[GRASS-user] grass run_command within python/grass script

Group,

I am running a grass-python script but when i replace a variable argument within the command the script does not work…

The following works…
grass.run_command(‘r.in.gdal’, input=‘/home/alassane/Data/SanbornCD/tile/newconstr_mask.tif’, output=‘construction’)

But not this…
command=“‘r.in.gdal’, input='”+sys.argv[5]+“', output=‘construction’”
print 'r.in.gdal command argument… = %s\n ’ % command
grass.run_command(command)

print ‘r.in.gdal command argument… = %s\n ’ % command
r.in.gdal command argument… = ‘r.in.gdal’, input=’/home/alassane/Data/SanbornCD/tile/newconstr_mask.tif’, output=‘construction’

The following also works…
command =“/home/alassane/Programs/gdal/demclass1”+" “+infile1+” “+infile2+” “+sys.argv[3]+” "+outfile
os.system(command)

Your help is appreciated.

Regards,

Alassane

Hello,

You try to pass a string as an argument to run_command, which will not work.
If you want to change the input, change that string only:
for m in maps:
    grass.run_command('r.in.gdal', input=m, output=m)

or, as in your example:
grass.run_command('r.in.gdal', input=sys.argv[5], output='your_map')

For command line parsing, I recommend using GRASS parser or argparse.
For string formatting, use .format() instead of %, which is deprecated.
(https://docs.python.org/2/library/stdtypes.html#str.format)

Laurent

2016-08-11 15:04 GMT-05:00 alassane toure <atourej@gmail.com>:

Group,
I am running a grass-python script but when i replace a variable argument
within the command the script does not work....

The following works...
grass.run_command('r.in.gdal',
input='/home/alassane/Data/SanbornCD/tile/newconstr_mask.tif',
output='construction')

But not this...
command="'r.in.gdal', input='"+sys.argv[5]+"', output='construction'"
print 'r.in.gdal command argument... = %s\n ' % command
grass.run_command(command)

print 'r.in.gdal command argument... = %s\n ' % command
r.in.gdal command argument... = 'r.in.gdal',
input='/home/alassane/Data/SanbornCD/tile/newconstr_mask.tif',
output='construction'

The following also works...
command ="/home/alassane/Programs/gdal/demclass1"+" "+infile1+" "+infile2+"
"+sys.argv[3]+" "+outfile
os.system(command)

Your help is appreciated.

Regards,
Alassane

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user