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 am unable to call shp2pgsql using python as I have to pass various options like (-s -a -i) and I am not getting the required output ..

Method I am using is

p1 = subprocess.Popen(["cmd", "/C", "shp2pgsql","-a","-s 4326","roads.shp" "roads", ">","roads.sql"],stdout=subprocess.PIPE)


as my intension is to add the features of the shapefile into the already existing table in the database

I am done with the environment settings.. roads.shp is the name of the shapefile roads is the table name and roads.sql is the sql file name..

share|improve this question
Please consider accepting some answers to your previous questions. With a 0% accept rate, people might not be willing to put effort into answering your questions anymore. – underdark Oct 13 '11 at 11:47
up vote 3 down vote accepted

This seems a bit convoluted. Why not use os.system instead? Just write out / manipulate the command as a string and feed it to os.system.

share|improve this answer

I suggest you can use the following codes:

p1 = subprocess.Popen(["cmd", "/C", "shp2pgsql","-a","-s 4326","roads.shp", "roads"], stdout=subprocess.PIPE)
stdout, stderr = p1.communicate()
with open('roads.sql', 'w') as f:

Another alternative method (if you want to import this into postgresql database):

p1 = subprocess.Popen(["cmd", "/C", "shp2pgsql","-a","-s 4326","roads.shp", "roads"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(['psql',  '-d db'], stdin=p1.stdout, 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr= p2.communicate()


share|improve this answer

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.