I have a polyline shapefile. The shapefile has a field called "NAME" enter image description here

I would like to make a loop over each line (A,B,C,D,...).

This is the python code for only first line named "A"

profileLine = WORK_DIRECTORY + r'\CrossSections_onlyA.shp' profileTargets = WORK_DIRECTORY + r'\DTM.tif' OutTable = WORK_DIRECTORY + r'\OutTableA.dbf' arcpy.StackProfile_3d(profileLine, profileTargets, OutTable)

I don't know how to do it over all of them with a loop. Also I would like to save the table with the name of each Line. for example: OutTableA.dbf, OutTableB.dbf, OutTableC.dbf and so on.

  • 1
    What happens when you run the code that you have presented? In any event, I think your starting point should be to use arcpy.da.SearchCursor().
    – PolyGeo
    Oct 10, 2018 at 8:10

1 Answer 1


Use the da.SearchCursor to iterate over each line/row of the profilelines and create a feature layer for each using objectid. Then pass this feature layer to StackProfile. One output folder will also be created for each output table.

import arcpy,os

WORK_DIRECTORY = r'C:\somefolder'
arcpy.env.workspace = WORK_DIRECTORY
arcpy.env.overwriteOutput = True

profileLine = r'CrossSections_onlyA.shp' 
profileTargets = r'DTM.tif'

with arcpy.da.SearchCursor(profileLine,['OID@','Name']) as cursor:
    for oid,name in cursor:
        sql = """{0} = {1}""".format(arcpy.Describe(profileLine).OIDFieldName, oid)
        arcpy.MakeFeatureLayer_management(in_features=profileLine, out_layer='templine', 
        OutTable = "OutTable{0}.dbf".format(name)
        OutFolder = os.path.join(arcpy.env.workspace,name)
        arcpy.StackProfile_3d('templine', profileTargets, os.path.join(OutFolder,OutTable))
  • Thank you so much. it works but ".dbf" was necessary.
    – Anahita Kp
    Oct 10, 2018 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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