I'm relatively new to python scripting and I'm trying to batch process a number of files, with the first command being to convert formats.

gdal_translate -of KEA NDVI_20160101.tif NDVI_20160101.KEA

works perfectly when inputting the command directly into the terminal but when I copy and paste the command into a text editor and try and run the script from the destination with:

python ./SCRIPT_NAME.py

I get SyntaxError: invalid syntax

  • What happens when you run your SCRIPT_NAME.py from an IDE like IDLE? I would expect it to tell you the line on which you have a syntax error. – PolyGeo Sep 24 '16 at 1:41

os.system() and os.popen() have been deprecated since Python 2.6 in favor of the subprocess module (for various reasons), see Calling gdal utilities from within python using subprocess.

import subprocess
subprocess.call('gdal_translate -of AAIGrid KEA NDVI_20160101.tif NDVI_20160101.KEA', shell=True)

You can't do this directly like that. If you want to run command in Python you should use:

import os
os.system("gdal_translate -of KEA NDVI_20160101.tif NDVI_20160101.KEA ")
  • This would work but the consensus is to now use subprocess.call – Nathan Thomas Oct 21 '16 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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