· 7 years ago · Dec 10, 2018, 07:46 AM
1#**********************************************************************
2# Description:
3# Tests if a field exists and outputs two booleans:
4# Exists - true if the field exists, false if it doesn't exist
5# Not_Exists - true if the field doesn't exist, false if it does exist
6# (the logical NOT of the first output).
7#
8# Arguments:
9# 0 - Table name
10# 1 - Field name
11# 2 - Exists (boolean - see above)
12# 3 - Not_Exists (boolean - see above)
13#
14# Created by: ESRI
15#**********************************************************************
16
17# Standard error handling - put everything in a try/except block
18#
19try:
20
21 # Import system modules
22 import sys, string, os, arcgisscripting
23
24 # Create the Geoprocessor object
25 gp = arcgisscripting.create()
26
27 # Get input arguments - table name, field name
28 #
29 in_Table = gp.GetParameterAsText(0)
30 in_Field = gp.GetParameterAsText(1)
31
32 # First check that the table exists
33 #
34 if not gp.Exists(in_Table):
35 raise Exception, "Input table does not exist"
36
37 # Use the ListFields function to return a list of fields that matches
38 # the name of in_Field. This is a wildcard match. Since in_Field is an
39 # exact string (no wildcards like "*"), only one field should be returned,
40 # exactly matching the input field name.
41 #
42 fields = gp.ListFields(in_Table, in_Field)
43
44 # If ListFields returned anything, the Next operator will fetch the
45 # field. We can use this as a Boolean condition.
46 #
47 field_found = fields.Next()
48
49 # Branch depending on whether field found or not. Issue a
50 # message, and then set our two output variables accordingly
51 #
52 if field_found:
53 gp.AddMessage("Field %s found in %s" % (in_Field, in_Table))
54 gp.SetParameterAsText(2, "True")
55 gp.SetParameterAsText(3, "False")
56 else:
57 gp.AddMessage("Field %s not found in %s" % (in_Field, in_Table))
58 gp.SetParameterAsText(2, "False")
59 gp.SetParameterAsText(3, "True")
60
61
62# Handle script errors
63#
64except Exception, errMsg:
65
66 # If we have messages of severity error (2), we assume a GP tool raised it,
67 # so we'll output that. Otherwise, we assume we raised the error and the
68 # information is in errMsg.
69 #
70 if gp.GetMessages(2):
71 gp.AddError(GP.GetMessages(2))
72 else:
73 gp.AddError(str(errMsg))