0

I would like to perform the arcpy.CreateFeatureclass_management tool to copy a template shapefile several times to create the same shapefile for multiple years. I would like the output files to be "neb_precip_2008.shp","neb_precip_2009.shp","neb_precip_2010.shp"... up until 2016. I understand it should be looped through, but I want to learn how to do this without a list. I'm still in the beginning stage of python learning.

try:
  immport arcpy
  from arcpy import env
  env.workspace = r"C:\Users\Documents"

  out_path = r"C:\Users\Documents"
  out_name = "neb_precip_2008.shp"
  geometry_type = "POINT"
  template = "practice_dataPrecip2008Readings.shp"
  has_m = "DISABLED"
  has_z = "DISABLED"

spatial_reference = arcpy.Describe("C:\Users\Documents\practice_dataPrecip2008Readings.shp").spatialReference

arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template, has_m, has_z, spatial_reference)

except:
  print "Script failed to complete"
  print arcpy.GetMessages(2)
  • Even though you are a beginner, looping through a list is not only one of the more common tasks as a Python programmer, but one of the more simple and explicit ways to store data – ziggy Sep 20 '16 at 18:29
  • Please leave the try/except statements out of code snippets that you are testing prior to posting because they can mask error messages that might be helpful when debugging. – PolyGeo Sep 20 '16 at 19:34
2

You could use a number loop using a range.

Try:

for year in range(2008, 2017):
  out_name = "neb_precip_{}.shp".format(year)
  arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template, has_m, has_z, spatial_reference)

(As an aside, I tend to like lists since they are more explicit.)

0

I personally do not use while loops, but for the chance that you find it easier in this case without using a list

year = 2008
while year < 2017:
    out_name = "neb_precip_{}.shp".format(year)
    arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template, has_m, has_z, spatial_reference)
    year+=1

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.