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 trying to convert MapINFO tabs to ESRI Shapefiles using FW Tools. I downloaded the tools and can run the FW Tools Shell and convert no problem:

ogr2ogr -f "ESRI Shapefile" E:\CABWorking\MapINFO2SHP\SHP\LincsBoundary.shp E:\CABWorking\MapINFO2SHP\MapINFOData\

In the shell you can type python and use the shell as a python interpreter. Awesome, but I need this to work as I want to convert a ton of MapINFO files at once.

My problem is: I can't figure out how to get the following code to work in a python environment. I've been searching all morning and just have no clue. Any help would be awesome!


share|improve this question
The FW Tools binaries are several years old. If you are on Windows try OSGeo4W instead. – Ragi Yaser Burhum Sep 17 '12 at 14:34
possible duplicate of How to resample a batch of rasters using OGR/GDAL? – iant Sep 17 '12 at 14:36
up vote 6 down vote accepted

In Python the os module provides a quick and dirty way to run a command line argument such as the one you posted as part of a larger script as outlined in the Python documentation

import os
command = "ogr2ogr -f \"ESRI Shapefile\" E:\\CABWorking\\MapINFO2SHP\\SHP\\LincsBoundary.shp E:\\CABWorking\\MapINFO2SHP\\MapINFOData\\"

Note the use of backslashes to escape characters such as " and \ in the command string.

As I said, this is quick and dirty, the Pythonic way to do this is to use the Subprocess library which provides the same functionality but in a more stable framework, and with some more features:

import os
command = "ogr2ogr -f \"ESRI Shapefile\" E:\\CABWorking\\MapINFO2SHP\\SHP\\LincsBoundary.shp E:\\CABWorking\\MapINFO2SHP\\MapINFOData\\"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000) 
#the third command suppresses the opening of a new window for each process
#and can be removed if this is not the desired outcome
process.wait() #waits for one process to complete before launching another one

I used this answer to solve my issue. Thanks a ton @sgrieve. The final code ended up being:

import os, glob
inputDir = raw_input("==> ") # Had to have this code compatible with 2.3.4
outputDir = raw_input("==> ") # So that meant no easy file dialog boxes
TabFiles = glob.glob(str(inputDir) + '/*.tab')
for TabFile in TabFiles:
    TabFileName = TabFile[(TabFile.rfind("\\"))+1:(TabFile.rfind("."))]
    command = 'ogr2ogr -f "ESRI Shapefile" ' + outputDir + "/" + TabFileName + '.shp ' + TabFile

I'd then open the FWTools Shell, enter python and call the script with:

share|improve this answer
I really like this solution, but when run it doesn't work. I think it's because python is trying to run the command in CMD as oppose to the FW Tools Shell. Maybe I need to bring the shell into CMD as well? – Cody Brown Sep 17 '12 at 15:04
Yes, you will need to import the FW tools shell into the command prompt. The process is outlined here… for importing python to the command shell, but the process should be the same for FW Tools. – sgrieve Sep 17 '12 at 15:55
Thanks a ton. Took me a little bit to get it going, but it now runs pretty smoothly thanks to your help! I'll paste the code in your answer. – Cody Brown Sep 18 '12 at 14:10

use this code with entering its folder with your command line:

for %f in (*.tab) do ogr2ogr -f "ESRI Shapefile" %~new.shp %f

i hope it helps you...

share|improve this answer
This seems great. Where about's would I specify the input and output directory? – Cody Brown Sep 17 '12 at 15:05

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.