-5

Ok so as you can see to my question I'm a total newb at python. I'm building a python script and basically I want it to execute this line


/Library/Frameworks/GDAL.framework/Programs/ogr2ogr -f "GeoJSON" output.json input.shp

How do I get python to execute this as if I was typing it in my terminal?
Thanks

climboid
  • 6,776
  • 14
  • 42
  • 68
  • Why was this question down voted? Is it not good enough for stack overflow standards? – climboid Feb 18 '13 at 20:32
  • I didn't downvote, but my guess is that it's because it's a duplicate of previous stack overflow questions like [this one](http://stackoverflow.com/questions/450285/executing-command-line-programs-from-within-python), which could easily be found by (for example) googling "launch command line Python" – David Robinson Feb 18 '13 at 20:52

2 Answers2

6
import os
os.system('/Library/Frameworks/GDAL.framework/Programs/ogr2ogr -f "GeoJSON" output.json input.shp')

More recently, it is recommended to use the subprocess package:

subprocess.call(['/Library/Frameworks/GDAL.framework/Programs/ogr2ogr', '-f',
                 '"GeoJSON"', 'output.json', 'input.shp'])
David Robinson
  • 71,331
  • 13
  • 150
  • 174
2

I cannot comment, but would like to add to the above answer:
The subprocess package allows for a return handle by which you can determine if the command was executed successfully. This may be important later in your script:

import subprocess  
COMMAND = '/Library/Frameworks/GDAL.framework/Programs/ogr2ogr -f "GeoJSON" output.json input.shp'  
return_code = subprocess.call(COMMAND, shell=True)  
Schorsch
  • 7,321
  • 5
  • 34
  • 64