<?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 Re: For loop not looping in ArcPy process in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539270#M42147</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure this will work, but you could try changing line 30 to:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="_jivemacro_uid_1426603652551470 jive_macro_code jive_text_macro" data-renderedposition="29_8_912_16" jivemacro_uid="_1426603652551470" modifiedtitle="true"&gt;&lt;P&gt;arcpy.env.workspace = r"G:\\GISAdmin\\Database_Management\\Compress.sde"+"\\"+d&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 17 Mar 2015 14:59:54 GMT</pubDate>
    <dc:creator>JoshuaChisholm</dc:creator>
    <dc:date>2015-03-17T14:59:54Z</dc:date>
    <item>
      <title>For loop not looping in ArcPy process</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539269#M42146</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I put together this python script to compress databases.&amp;nbsp; There seems to be an issue with the for loop where it correctly loops through the list of database names throughout the script except within the ArcPy process where a new database connection is created and the database name is used to define which database to connect to.&amp;nbsp; While all "print" indicators appear to be using the desired database, the database connection is repeatedly created with only the first database in the list of database names and, therefore, the first database is the list is compressed over and over, while the remaining databases are not compressed (confirmed in the SQL sde compress log).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any advise and help is greatly appreciated! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import modules
import arcpy
import datetime
import time
import smtplib
import os

# Find and store process beginning time
starttime = datetime.datetime.now()

# Local variables
saConnect = r"G:\\GISAdmin\\Database_Management\\Compress.sde"
databaseName = ["devservices", "electric", "fire", "gis", "parks", "police", "publicworks", "water_services"]

# Delete database connection file if exists
if os.path.exists(saConnect):
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(saConnect)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("\n\n\nExisting database connection deleted.")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("\n\n\nNo database connection found. I'll just make one for all y'all.")

for d in databaseName:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("\n\nBeginning compression processes for " + str(d) + " database.")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create database connection file
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CreateDatabaseConnection_management(r"G:\\GISAdmin\\Database_Management", "Compress.sde", "SQL_SERVER", "gis3", "DATABASE_AUTH", "sa", "******", "SAVE_USERNAME", d, "", "TRANSACTIONAL", "sde.DEFAULT", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("Database connection file created for " + str(d))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set the workspace
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = r"G:\\GISAdmin\\Database_Management\\Compress.sde"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("Workspace defined")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set a variable for the workspace
&amp;nbsp;&amp;nbsp;&amp;nbsp; workspace = arcpy.env.workspace

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get a list of connected users
&amp;nbsp;&amp;nbsp;&amp;nbsp; users = arcpy.ListUsers(workspace)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("Users found")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get a list of usernames of users currently connected
&amp;nbsp;&amp;nbsp;&amp;nbsp; viewUsers = [user.Name for user in users]
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("View Users")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get and format connection times for each user
&amp;nbsp;&amp;nbsp;&amp;nbsp; connectTimeFormat = datetime.datetime.strftime(user.ConnectionTime, "%Y-%m-%d %H:%M:%S")
&amp;nbsp;&amp;nbsp;&amp;nbsp; viewConnectTime = [connectTimeFormat for user in users]
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("\n\nUSERS FOR " + str(d))
&amp;nbsp;&amp;nbsp;&amp;nbsp; template = "{0:4}|{1:15}"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print template.format("USER", "CONNECTION TIME") # header
&amp;nbsp;&amp;nbsp;&amp;nbsp; for userWho, userWhen in zip(viewUsers, viewConnectTime):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print userWho, userWhen

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Block new connections to the database
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AcceptConnections(workspace, False)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("\n\nNew connections to database blocked")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Disconnect all users from the database
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DisconnectUser(workspace, "ALL")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("Database users disconnected")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Run the compress tool
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Compress_management(workspace)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("\n\nDatabase compression complete")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Allow the database to begin accepting connections again
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AcceptConnections(workspace, True)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("Allowing new connections to database")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Delete database connection file
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.exists(workspace):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(workspace)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ("Connection for " + str(d) + " deleted.")

# Find and store process beginning time
endtime = datetime.datetime.now()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:23:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539269#M42146</guid>
      <dc:creator>MarieCline_Delgado</dc:creator>
      <dc:date>2021-12-11T23:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: For loop not looping in ArcPy process</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539270#M42147</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure this will work, but you could try changing line 30 to:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="_jivemacro_uid_1426603652551470 jive_macro_code jive_text_macro" data-renderedposition="29_8_912_16" jivemacro_uid="_1426603652551470" modifiedtitle="true"&gt;&lt;P&gt;arcpy.env.workspace = r"G:\\GISAdmin\\Database_Management\\Compress.sde"+"\\"+d&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Mar 2015 14:59:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539270#M42147</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2015-03-17T14:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: For loop not looping in ArcPy process</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539271#M42148</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When you make directory path strings, you can either use the double backslash to escape the special character, or you can use the r prefix to designate the string as "raw" so it will not consider a single backslash as a special character. You have combined both methods and will probably not work. You need to do one or the other. Personally, I like to use the raw string method:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14266105596657271 jive_text_macro" data-renderedposition="71_8_912_16" jivemacro_uid="_14266105596657271" modifiedtitle="true"&gt;&lt;P&gt;arcpy.env.workspace = r"G:\GISAdmin\Database_Management\Compress.sde"&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Mar 2015 16:43:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539271#M42148</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2015-03-17T16:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: For loop not looping in ArcPy process</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539272#M42149</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just FYI.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;arcpy.ClearWorkspaceCache_management() was the answer to the problem.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The workspace was holding onto .sde connection properties.&amp;nbsp;&amp;nbsp; Clearing the workspace cache resolved that. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Apr 2015 12:46:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-not-looping-in-arcpy-process/m-p/539272#M42149</guid>
      <dc:creator>MarieCline_Delgado</dc:creator>
      <dc:date>2015-04-17T12:46:37Z</dc:date>
    </item>
  </channel>
</rss>

