<?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 arcpy UpdateCursor &amp;amp;amp;quot;cannot acquire a lock&amp;amp;amp;quot; in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252765#M19469</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm pretty new to Python, and I'm trying to put together a script which will, after doing a bunch of other stuff, update a column conditionally based on the contents of another column.&amp;nbsp; The relevant code is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(r"C:\TEMP\storesgathered.shp")
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("CenType") == "REG":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(r"C:\TEMP\storesgathered.shp", "Adj_X", 8)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(r"C:\TEMP\storesgathered.shp", "Adj_X", 9)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
del row, rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So basically it's supposed to look at each row in "CenType", check if it equals "REG", and calculate out "Adj_X" with the relevant value.&amp;nbsp; The calculate field values for "Adj_X" are just dummy values at this point, to see if I can get it working.&amp;nbsp; The final script will have a bunch of elifs as well, to account for all of the possible values in "CenType".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The problem is, when I run it, I get this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Runtime error &amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock. [The table storesgathered.shp is being written by another process.] Failed to execute (CalculateField). &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is a really stubborn lock too - removing the layer from the TOC isn't enough; I need to exit all the way out of ArcMap to release it.&amp;nbsp; I've tried running the snippet of code externally from IDLE as well, but the shapefile still gets locked.&amp;nbsp; I've even tried exporting the shapefile to an in_memory entity, but it still gets locked.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm sort of out of ideas here.&amp;nbsp; Is there something obvious I'm doing wrong, or is there another approach that will get me the same results?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 12:31:27 GMT</pubDate>
    <dc:creator>UrbisMelbourne</dc:creator>
    <dc:date>2021-12-11T12:31:27Z</dc:date>
    <item>
      <title>arcpy UpdateCursor &amp;amp;quot;cannot acquire a lock&amp;amp;quot;</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252765#M19469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm pretty new to Python, and I'm trying to put together a script which will, after doing a bunch of other stuff, update a column conditionally based on the contents of another column.&amp;nbsp; The relevant code is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(r"C:\TEMP\storesgathered.shp")
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("CenType") == "REG":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(r"C:\TEMP\storesgathered.shp", "Adj_X", 8)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(r"C:\TEMP\storesgathered.shp", "Adj_X", 9)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
del row, rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So basically it's supposed to look at each row in "CenType", check if it equals "REG", and calculate out "Adj_X" with the relevant value.&amp;nbsp; The calculate field values for "Adj_X" are just dummy values at this point, to see if I can get it working.&amp;nbsp; The final script will have a bunch of elifs as well, to account for all of the possible values in "CenType".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The problem is, when I run it, I get this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Runtime error &amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock. [The table storesgathered.shp is being written by another process.] Failed to execute (CalculateField). &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is a really stubborn lock too - removing the layer from the TOC isn't enough; I need to exit all the way out of ArcMap to release it.&amp;nbsp; I've tried running the snippet of code externally from IDLE as well, but the shapefile still gets locked.&amp;nbsp; I've even tried exporting the shapefile to an in_memory entity, but it still gets locked.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm sort of out of ideas here.&amp;nbsp; Is there something obvious I'm doing wrong, or is there another approach that will get me the same results?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:31:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252765#M19469</guid>
      <dc:creator>UrbisMelbourne</dc:creator>
      <dc:date>2021-12-11T12:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy UpdateCursor "cannot acquire a lock"</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252766#M19470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The short answer to your problem is that you can't use CalculateField on the same data that you're already accessing through an update cursor. It isn't appropriate in this context anyway; CalculateField is meant for performing the same calculation for every row in the layer. Here is the logic you should use for changing the attributes in an update cursor:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("CenType") == "REG":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Adj_X = 8)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Adj_X = 9)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:31:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252766#M19470</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2021-12-11T12:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy UpdateCursor "cannot acquire a lock"</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252767#M19471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Don't use CalculateField_management inside the cursor. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;CalculateField_management essentially sets up a new cursor on the file: your file, however, is already locked by your UpdateCursor named rows.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You already have the row in hand, and you can directly update the value in any of its fields:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;if row.getValue("CenType") == "REG": row.Adj_X = 8&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 May 2012 11:26:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252767#M19471</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-05-21T11:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy UpdateCursor "cannot acquire a lock"</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252768#M19472</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Awesome, works a treat, thanks guys.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 May 2012 03:56:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252768#M19472</guid>
      <dc:creator>UrbisMelbourne</dc:creator>
      <dc:date>2012-05-22T03:56:44Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy UpdateCursor &amp;amp;quot;cannot acquire a lock&amp;amp;quot;</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252769#M19473</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Bruce, you may want to remove the extra&amp;nbsp; )&amp;nbsp; at the end of row 3 and 5.&amp;nbsp; For future viewers.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2016 15:20:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-updatecursor-amp-amp-quot-cannot-acquire-a/m-p/252769#M19473</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2016-09-28T15:20:03Z</dc:date>
    </item>
  </channel>
</rss>

