[GIS] Checking for number of shapefiles in directory using Python

arcpypythonshapefile

I want to test:

  1. to see whether or not ONE AND ONLY ONE shapefile (which will be based on a filename wildcard) exists in a directory.
  2. Add these shapefiles to a list
  3. Use this list variable as an input to merge_managment function.

I already know how to walk through directories and pick out those filenames but not how to do the above.

I was thinking something like:

rootDir = "\homePath"
featureinput1 = "path"
newOnlyShpList = []
for dirPath, dirNames, fileNames in os.walk(rootDir):
    for file in fileNames:
       if fnmatch.fnmatch(file, "*" + "2012" + "*" + ".shp")== True ##AND IF ONE SHAPEFILE EXISTS BY ITSELF IN DIRECTORY:##
          ##append to newOnlyShpList
          arcpy.Merge_managment([featureinput1, newOnlyShpList], "OutputHere")
       elif
          ##ignore if greater than one shapefile

All I know about is the arcpy .exists function but all that does is return a boolean on whether any shapefile exists. Maybe I need to check just for 1 .shp extension and it has nothing to do with arcpy?

Best Answer

Check the glob library:

import os
import glob
path = "c:\\my\\dir"

flist = glob.glob(os.path.join(path,"shapefile*"))  <- Notice the *

>>> flist 
["c:\\my\\dir\shapefile.prj","c:\\my\\dir\shapefile.shp",...]

If you want just the fname without the path attached:

 os.path.basename(flist[0]) 
 >>> shapefile.prj

if you want just the file name without the extension:

os.path.basename(flist[0]).split(".")[0]
>>> shapefile

Also if the list is empty, then the file doesn't exist:

if flist not:
    print "File not found"
    break #or os.exit(1)

Edit: Also to check for the only filename in path :

import os
import glob
path = "c:\\my\\dir"

flist = glob.glob(os.path.join(path,"shapefile*"))  <- Notice the *
mlist = []
for file in flist:
    if os.path.basename(file).split(".")[0] not in mlist:
        mlist.append(os.path.basename(file).split(".")[0])
if len(mlist)>1: #(More than filename)
    break

To check if only one shapefile in the dir:

 def file_check(path):
      """Returns true if in path there is only one basename"""
      import os
      mlist = [] 
      for file in os.listdir(path):
          if os.path.basename(file).split(".")[0] not in mlist:
                mlist.append(os.path.basename(file).split(".")[0]
   if len(mlist)>1:
            return True
    else:
            return False
Related Question