Methods of Merging layers/shapefiles using OGR in python
<p>I am new to python and am trying to get more comfortable automating GIS tasks with python. Any help is appreciated</p>

<p>I have two layers containing points that I am trying to merge together into one single layer, using Ogr in python. Below is code I found on a website, however it is giving me an error</p>

<blockquote>
  <p>AttributeError: 'NoneType' object has no attribute 'GetLayer'</p>
</blockquote>

<p>The line I believe that is causing this error is: </p>

<blockquote>
  <p>ds = ogr.Open(directory + file)</p>
</blockquote>

<p>I am wondering why nothing is generated at this step, and I am also wondering if there is a different/better approach to merging layers using gdal/ogr python</p>

<pre><code>outputMergefn = 'Merge.shp'
directory = "C:/Users/Robin/Documents/Python Final Project/Final_Project/Output"
filestartswith = 'C'
FileEndsWith = '.shp'
drivername = 'ESRI Shapefile'
geometrytype = ogr.wkbMultiPoint
ptdriver = ogr.GetDriverByName('ESRI Shapefile')

if os.path.exists(outputMergefn):
    ptdriver.DeleteDataSource(outputMergefn)
out_ds = ptdriver.CreateDataSource(outputMergefn)
out_layer = out_ds.CreateLayer(outputMergefn, geom_type = geometrytype)

filelist = os.listdir(directory)
for file in filelist:
    if file.startswith(filestartswith) and file.endswith(FileEndsWith):
        print file
        ds = ogr.Open(directory + file)
        if ds is None:
            print "This is None"
        lyr = ds.GetLayer()
        for feat in lyr:
            out_feat = ogr.Feature(out_layer.GetLayerDefn())
            out_feat.SetGeometry(feat.GetGeometryRef().Clone())
            out_layer.CreateFeature(out_feat)
            out_layer.SyncToDisk()
</code></pre>