Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import modules
- import arcpy
- import sys
- import traceback
- # Set local variables
- indata = arcpy.GetParameterAsText(0)
- fieldName = "PrintKey_GIS"
- try:
- # check if the PrintKey field exists
- if arcpy.ListFields(indata, fieldName): #if field exists, evaluates to true
- arcpy.AddMessage("PrintKey_GIS field exists")
- else:
- # Add a new field of that name
- arcpy.AddField_management(indata, fieldName, "TEXT", "", "",25, "PrintKey_GIS" , "NULLABLE", "", "",)
- arcpy.AddMessage("PrintKey_GIS field does not exist")
- except Exception as e:
- arcpy.AddMessage(arcpy.GetMessages())
- print "Adding new field first time"
- arcpy.AddField_management(fc, "NewField", "TEXT")
- print "Adding new field second time"
- arcpy.AddField_management(fc, "NewField", "TEXT")
- print "Adding new field third time"
- arcpy.AddField_management(fc, "NewField", "TEXT")
- Adding new field first time
- Adding new field second time
- Adding new field third time
- Executing (Add Field): AddField C:Users<username>DocumentsArcGISDefault.gdbfc NewField TEXT # # # # NULLABLE NON_REQUIRED #
- Start Time: Tue Aug 21 11:01:38 2018
- WARNING 000012: NewField already exists
- Succeeded at Tue Aug 21 11:01:38 2018 (Elapsed Time: 0.08 seconds)
- Executing (Calculate Field): CalculateField C:Users<username>DocumentsArcGISDefault.gdbfc NewField 'blah' PYTHON #
- Start Time: Tue Aug 21 11:01:38 2018
- Succeeded at Tue Aug 21 11:01:38 2018 (Elapsed Time: 0.12 seconds)
- if len(arcpy.ListFields(fc, fieldName)):
- # Field exists. Do something.
- else:
- # Field does not exist. Do something else.
- import arcpy
- import os
- arcpy.env.workspace = r"C:UsersSanjayDocumentsArcGISSimilarPoints by attribute in polygonmygdb.gdb" #Parameter1
- def FieldExist(featureclass, fieldname): #* Start if function to check if field exist */
- fieldList = arcpy.ListFields(featureclass, fieldname)
- fieldCount = len(fieldList)
- if (fieldCount == 1):
- return True
- else:
- return False #* End of function to check if field exist */
- datasets = arcpy.ListDatasets(feature_type='feature')
- datasets = [''] + datasets if datasets is not None else []
- ListOfFieldNeeded = ["ID","Creator","checker","Approver"]#parameter2 #List of field to check if not exist add it.
- for Afield in ListOfFieldNeeded: #will check each fc in gdb and add field
- for ds in datasets:
- for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
- if not FieldExist(fc,Afield):
- #print "{} Not Exist in featureclass {}".format(Afield,fc)
- arcpy.AddField_management(fc,Afield,"TEXT")
- print "Field {} added in fc {}".format(Afield,fc) #Addmessage to print model builder environment
- #you can add your script validation using else or exception occures
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement