Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/", line
76, in <module>
  File "C:/Users/Simon/Documents/", line
73, in main
  File "C:/Users/Simon/Documents/", line
31, in import_tifs
  File "C:\GRASS64\etc\python\grass\script\", line
189, in run_command
    ps = start_command(*args, **kwargs)
  File "C:\GRASS64\etc\python\grass\script\", line
167, in start_command
    args = make_command(prog, flags, overwrite, quiet,
verbose, **options)
  File "C:\GRASS64\etc\python\grass\script\", 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.

share|improve this question
up vote 3 down vote accepted

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

share|improve this answer
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 Oct 29 '12 at 22:12
what about trying os.system('gdalwarp ... ')? – dmci Oct 30 '12 at 8:02
Thanks - this has worked! Could you tell me why os.system worked and os.sys didn't? – si_2012 Oct 30 '12 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
share|improve this answer
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 Oct 30 '12 at 8:39
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." – Luke Oct 30 '12 at 19:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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