Here is the tool like it is it in ESRI. the input driver could be modified to your needs. Also no projection is enforced on the output currently.
def erase(baseFile, eraseFile, outFile): #this class performs the erase tool from ARCGIS// erasefile must be polygon
from osgeo import ogr
driver = ogr.GetDriverByName('Mapinfo File')
driver2 = ogr.GetDriverByName('ESRI Shapefile')
feat1 = driver.Open(baseFile, 0)
if feat1 is None:
print 'Could not open feat1.tab'
feat2 = driver2.Open(eraseFile, 0)
if feat2 is None:
print 'Could not open feat2.tab'
feat1Layer = feat1.GetLayer()
feat2Layer = feat2.GetLayer()
feature2 = feat2Layer.GetFeature(0)
geomfeat2 = feature2.GetGeometryRef()
# create a new data source and layer
fn = outFile
outDS = driver.CreateDataSource(fn)
if outDS is None:
print 'Could not create file'
outLayer = outDS.CreateLayer('roads', geom_type=ogr.wkbLineString)
# get the FeatureDefn for the output shapefile
featureDefn = outLayer.GetLayerDefn()
#loop through input layer
for inFeature in feat1Layer:
# create a new feature
outFeature = ogr.Feature(featureDefn)
# copy the attributes and set geometry
#get Geometry of feature and apply the difference method on that feature
inGeom = inFeature.GetGeometryRef()
GeomDiff = inGeom.Difference(geomfeat2)
#Set geometry of the outfeature
# add the feature to the output layer