-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

2
  • Why was this question down voted? Is it not good enough for stack overflow standards? Commented Feb 18, 2013 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, which could easily be found by (for example) googling "launch command line Python" Commented Feb 18, 2013 at 20:52

2 Answers 2

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'])
Sign up to request clarification or add additional context in comments.

3 Comments

btw. does python wait for the execution to finish or just fork the process to the os?
@tomasz: os.system waits for it to finish. If you want to fork the process, use subprocess.Popen.
Thanks David! I can accept your anser in exactly 9 minutes =)
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)  

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.