<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Populate attribute field within geodatabase using feature class name in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144027#M11171</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Do you have anywhere I can start my search on finding a python script that autopopulates an attribute field based on feature class name for feature classes contained in the TOC of an MXD or even better, a geodatabase? I can???t seem to get a handle on this and have spent ages in the python forum. My start was something like this :-&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")
fcs = arcpy.ListFeatureClasses(mxd)
fcs.reset()
fc = fcs.Next()
while fc:
arcpy.CalculateField_management (fc, "FILENAME", '"' + fc + '"')
fc = fcs.Next()
print "done&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 22 Mar 2012 16:05:26 GMT</pubDate>
    <dc:creator>DanBignell</dc:creator>
    <dc:date>2012-03-22T16:05:26Z</dc:date>
    <item>
      <title>Populate attribute field within geodatabase using feature class name</title>
      <link>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144027#M11171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Do you have anywhere I can start my search on finding a python script that autopopulates an attribute field based on feature class name for feature classes contained in the TOC of an MXD or even better, a geodatabase? I can???t seem to get a handle on this and have spent ages in the python forum. My start was something like this :-&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")
fcs = arcpy.ListFeatureClasses(mxd)
fcs.reset()
fc = fcs.Next()
while fc:
arcpy.CalculateField_management (fc, "FILENAME", '"' + fc + '"')
fc = fcs.Next()
print "done&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Mar 2012 16:05:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144027#M11171</guid>
      <dc:creator>DanBignell</dc:creator>
      <dc:date>2012-03-22T16:05:26Z</dc:date>
    </item>
    <item>
      <title>Re: Populate attribute field within geodatabase using feature class name</title>
      <link>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144028#M11172</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Dan,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example on how to do this.&amp;nbsp; The below code will list all the feature classes in a geodatabase (including feature classes within feature datasets), insert a record into a dbf table, and update an existing field within the table called 'Names' with the feature class name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
env.workspace = r"C:\TEMP\Python\Test.gdb"

table = r"C:\TEMP\Python\FieldNames.dbf"

list = []

lstFCs = arcpy.ListFeatureClasses("*", "")
for fc in lstFCs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(fc)

del fc, lstFCs
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
lstDatasets = arcpy.ListDatasets("*")
for dataset in lstDatasets:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lstFCs = arcpy.ListFeatureClasses("*", "", dataset)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for fc in lstFCs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(fc)

for item in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.InsertCursor(table)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.newRow()
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Fields = item
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.insertRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Successfully inserted"

del row, rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:53:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144028#M11172</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T07:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Populate attribute field within geodatabase using feature class name</title>
      <link>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144029#M11173</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;JSkinn,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks very much for your reply. It was very informative. I have not managed to get it to work however, as I don't understand two points.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) Do you have to specify the specific table within the GDB, or can you run it on all of the feature classes within the geodatabase? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) I can't find where you specify the attribute field name within the code. YOu say mean to update an existing field called "Names" but I can't see where this is in the code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thakn you again for your help on this. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dna.B&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Mar 2012 07:13:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144029#M11173</guid>
      <dc:creator>DanBignell</dc:creator>
      <dc:date>2012-03-27T07:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Populate attribute field within geodatabase using feature class name</title>
      <link>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144030#M11174</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;1) Do you have to specify the specific table within the GDB, or can you run it on all of the feature classes within the geodatabase? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The table I'm writing all of the feature class names to is specified here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;table = r"C:\TEMP\Python\FieldNames.dbf"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the table exists within the geodatabase (not as a DBF), I can simply specify the name of that table.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;table = "FieldNames"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) I can't find where you specify the attribute field name within the code. YOu say mean to update an existing field called "Names" but I can't see where this is in the code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That was a type-o on my part.&amp;nbsp; The field name in the above example is called 'Fields', not 'Names'.&amp;nbsp; I'm calling this field using 'row.Fields = item'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for item in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.InsertCursor(table)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.newRow()
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;row.Fields = item&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.insertRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Successfully inserted"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:53:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144030#M11174</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T07:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Populate attribute field within geodatabase using feature class name</title>
      <link>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144031#M11175</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I actually got what I wanted from a colleague, but your thread gave us a lot of inspiration, so thank you very much. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FieldExist(featureclass, fieldname):
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldList = arcpy.ListFields(featureclass, fieldname)
&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldCount = len(fieldList)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (fieldCount == 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False

myField = "MAP"

import arcpy
from arcpy import env
env.workspace = "C:\Users\Danb\Desktop\PG30M.mdb"
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (FieldExist(fc, myField) == False):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc, myField, "TEXT")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc, myField, "'" + fc + "'", "PYTHON")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much JSkinn!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:54:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/populate-attribute-field-within-geodatabase-using/m-p/144031#M11175</guid>
      <dc:creator>DanBignell</dc:creator>
      <dc:date>2021-12-11T07:54:01Z</dc:date>
    </item>
  </channel>
</rss>

