3

I have hundreds of shapefiles that are stored in separate folders, which are nested within several other folders. Among the shapefiles I only need the ones that are named 'POINT*.shp'

How do I get these specific shapefiles so I can then add fields, calculate fields, etc.?

If there is a better option than os.walk, please tell. I'm only beginning to learn python, so this is very new to me.

  • What version of ArcMap/Python do you have access to? – Barbarossa Sep 20 '13 at 18:04
  • I'm using ArcMap 10.1 and Python 2.7 – S.S. Sep 20 '13 at 22:27
8

Use os.walk and then loop through the files appending to a list, where the filename starts with 'point' and ends with '.shp'.

import os
path = "D:/Files/GIS/Datafiles/"
shp_list = []
for dirpath, dirnames, files in os.walk(path):
    for f in files:
        if f.lower().endswith(".shp") and f.lower().startswith("point"):
            print f
            fullpath = os.path.join(dirpath, f)
            shp_list.append(fullpath)
  • Maybe less identation at "print" and below? Otherwise, very good. A regex doesn't help in this particular case but might in others. – sgillies Sep 20 '13 at 20:19
  • Thanks for your help Kevin. The only issue is that the shapefiles don't necessarily being with "point" -- sometimes it is in the middle of the name, and sometimes at the end. Is there a function that would say something like 'contains("point")'? – S.S. Sep 24 '13 at 22:56
  • @sgillies: thanks. I edited out the extra indentation. – KevinMayall Oct 3 '13 at 14:15
  • @S.S.: yes, just use if f.lower().find("point") <> -1 – KevinMayall Oct 3 '13 at 14:17
2

There is a similar question here, but this is only helpful if you have access to ArcMap 10.1 SP1. If so you could use the da.walk to look for shapefiles. If you want any feature class with POINT in the name, you could do the following:

import arcpy, os, fnmatch
# Set the workspace with user input
arcpy.env.workspace = "Your directory here"
inWorkspace = arcpy.env.workspace
pointList = []
for dirpath, dirnames, filenames in arcpy.da.Walk(inWorkspace, followlinks=True, datatype="FeatureClass"):
    for filename in filenames:
        # Add any field with "POINT" in the filename when creating list
        if fnmatch.fnmatch(filename, "*POINT*"):
            pointList.append(os.path.join(dirpath, filename))

This creates a list (pointList) of complete paths to shapefiles with "POINT" in the name within your specified directory. Alternatively you could include type="Point" in the arcpy.da.Walk() to search for only POINT feature classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.