I'm writing a Python script that aims to re-project various LANDSAT tiles into a common projection and then import the tiles into Grass. The script at present is shown below:


import os
import sys
import glob
import grass.script as grass

def import_tifs(dirpath):
    for dirpath, dirname, filenames in os.walk(dirpath):
        for tile in filenames:
        if tile.upper().endswith('.TIF'):
                full_path = os.path.join(dirpath, tile)     
                name = os.path.splitext(tile)[0]        
        rename = name[:-4] + '.' + name[-2]
        new_path = os.path.join(dirpath, rename)
        re_project = 'EPSG:32643'
                grass.message('Importing %s -> %s@%s...' % (tile, name, dirpath))               
                                  flags = 'e',
                                  input = full_path,
                                  output = name,
                                  quiet = True,
                                  overwrite = True)

def main():                                 
    if len(sys.argv) == 1:                          
        for directory in filter(os.path.isdir, os.listdir(os.getcwd())):
if __name__ == "__main__":

I'm having real trouble using the 'gdalwarp' command to reproject the LANDSAT tiles. I keep getting the following error:

Traceback (most recent call last):
  File "C:/Users/Simon/Documents/import_landsat3.py", line
76, in <module>
  File "C:/Users/Simon/Documents/import_landsat3.py", line
73, in main
  File "C:/Users/Simon/Documents/import_landsat3.py", line
31, in import_tifs
  File "C:\GRASS64\etc\python\grass\script\core.py", line
189, in run_command
    ps = start_command(*args, **kwargs)
  File "C:\GRASS64\etc\python\grass\script\core.py", line
167, in start_command
    args = make_command(prog, flags, overwrite, quiet,
verbose, **options)
  File "C:\GRASS64\etc\python\grass\script\core.py", line
124, in make_command
    raise ScriptError("'-' is not a valid flag")
grass.script.core.ScriptError: "'-' is not a valid flag"

I'm assuming this is related to '-t_srs' but I haven't been able to work out a way around the problem - can anyone suggest how to do this?

Many thanks.

2 Answers 2


Have you tried calling gdalwarp using os.sys? Check this post for more details on the topic.

  • Thanks - I've given this a try, but have got the error: TypeError: 'module' object is not callable What module do I need to import to use gdalwarp with os.sys in a Python script?
    – si_2012
    Commented Oct 29, 2012 at 22:12
  • what about trying os.system('gdalwarp ... ')?
    – dmci
    Commented Oct 30, 2012 at 8:02
  • 1
    Thanks - this has worked! Could you tell me why os.system worked and os.sys didn't?
    – si_2012
    Commented Oct 30, 2012 at 8:37

Try the subprocess module:

import subprocess
cmd = ['gdalwarp','-t_srs',re_project,full_path,new_path]
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

if exit_code: #Oops, something went wrong!
    raise RuntimeError(stderr)
else:print stdout
  • Thanks for this - I've managed to get the code to work using os.system as suggested by @dmci but I'm going to try using the subprocess module as I haven't come across it before.
    – si_2012
    Commented Oct 30, 2012 at 8:39
  • 1
    From the python docs for os.system: "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function."
    – user2856
    Commented Oct 30, 2012 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.