<?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 UnboundLocalError  using sequence in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020693#M59661</link>
    <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I have a script where I am parsing through all the MXD's on the server and getting the service properties of all the feature classes and writing to a csv file.&lt;/P&gt;&lt;P&gt;I am running into an issue in an else statement where it is&amp;nbsp;UnboundLocalError: local variable 'db' referenced before assignment.&lt;/P&gt;&lt;P&gt;I have tried changing the indentation of the sequence and when I put that inside the else statement I only get the first MXD in the folder written to the csv.&lt;/P&gt;&lt;P&gt;Below is the script. Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os, csv, socket

#Turn off ESRI geoprocessing logging so XML column does not fill up
arcpy.SetLogHistory(False)

arcpy.gp.overwriteOutput = True

ags = socket.gethostname()
print "ArcGISServer:" +     ags

def main(folder, outputfile):
    with open(outputfile, "wb") as f:
        w = csv.writer(f)
        header = ("MapDocument", "LayerName", "FeatureClass", "Database", "Server", "Version", "UserName", "Authentication", "ArcGISServer")
        w.writerow(header)
        rows = crawlmxds(folder)
        w.writerows(rows)

def crawlmxds(folder):
    for root, dirs, files in os.walk(folder):
        for f in files:
            if f.lower().endswith(".mxd"):
                mxdName = os.path.splitext(f)[0]
                mxdPath = os.path.join(root, f)
                mxd = arcpy.mapping.MapDocument(mxdPath)
                print mxdPath
                print mxdName
                for lyr in arcpy.mapping.ListLayers(mxd):
                    if lyr.supports("DATASOURCE"):
                        dSource = str(lyr.dataSource)
                        if '.sde' in dSource:
                            fcName = dSource.split('.sde\\')
                            print "Layer Name: " + lyr.name
                            print "Feature Class: " + fcName[1]
                        elif '.gdb' in dSource:
                            fcName = dSource.split('.gdb\\')
                            print "Layer Name: " + lyr.name
                            print "Feature Class: " + fcName[1]
##                        if lyr.supports("definitionQuery"):
##                            dq = lyr.definitionQuery
##                            print "Definition Query: " + lyr.definitionQuery
##                        else:
##                            print "NA"
                        if lyr.supports("SERVICEPROPERTIES"):
                            if lyr.serviceProperties["ServiceType"] != "SDE":
                                print "Service Type:  " + lyr.serviceProperties.get('ServiceType', 'N/A')
                                print "     URL:          " + lyr.serviceProperties.get('URL', 'N/A')
                                print "     Connection:   " + lyr.serviceProperties.get('Connection','N/A')
                                print "     Server:       " + lyr.serviceProperties.get('Server','N/A')
                                print "     Cache:        " + lyr.serviceProperties.get('Cache','N/A')
                                print "     User Name:    " + lyr.serviceProperties.get('UserName','N/A')
                                print "     Password:     " + lyr.serviceProperties.get('Password','N/A')
                                print ""
                            else:
                                print "Service Type:  " + lyr.serviceProperties.get('Service Type', 'N/A')
                                db = lyr.serviceProperties.get('Database', 'N/A')
                                print "     Database:       " + lyr.serviceProperties.get('Database', 'N/A')
                                svr = lyr.serviceProperties.get('Server', 'N/A')
                                print "     Server:         " + lyr.serviceProperties.get('Server', 'N/A')
                                srvc = lyr.serviceProperties.get('Service', 'N/A')
                                print "     Service:        " + lyr.serviceProperties.get('Service', 'N/A')
                                ver = lyr.serviceProperties.get('Version', 'N/A')
                                print "     Version:        " + lyr.serviceProperties.get('Version', 'N/A')
                                un = lyr.serviceProperties.get('UserName', 'N/A')
                                print "     User Name:      " + lyr.serviceProperties.get('UserName', 'N/A')
                                auth = lyr.serviceProperties.get('Authentication', 'N/A')
                                print "     Authentication: " + lyr.serviceProperties.get('Authentication', 'N/A')
                                print ""

                        seq = (mxdName, lyr.name, fcName[1], db, svr, ver, un, auth, ags);
                        yield seq
                del mxd

if __name__ == "__main__":
    folderPath = r"mxdpath" # or arcpy.GetParameterAsText(0)
    output = r"csvpath" # or arcpy.GetParameterAsText(1)
    main(folderPath, output)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Jan 2021 21:01:15 GMT</pubDate>
    <dc:creator>StevenCross</dc:creator>
    <dc:date>2021-01-27T21:01:15Z</dc:date>
    <item>
      <title>UnboundLocalError  using sequence</title>
      <link>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020693#M59661</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I have a script where I am parsing through all the MXD's on the server and getting the service properties of all the feature classes and writing to a csv file.&lt;/P&gt;&lt;P&gt;I am running into an issue in an else statement where it is&amp;nbsp;UnboundLocalError: local variable 'db' referenced before assignment.&lt;/P&gt;&lt;P&gt;I have tried changing the indentation of the sequence and when I put that inside the else statement I only get the first MXD in the folder written to the csv.&lt;/P&gt;&lt;P&gt;Below is the script. Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os, csv, socket

#Turn off ESRI geoprocessing logging so XML column does not fill up
arcpy.SetLogHistory(False)

arcpy.gp.overwriteOutput = True

ags = socket.gethostname()
print "ArcGISServer:" +     ags

def main(folder, outputfile):
    with open(outputfile, "wb") as f:
        w = csv.writer(f)
        header = ("MapDocument", "LayerName", "FeatureClass", "Database", "Server", "Version", "UserName", "Authentication", "ArcGISServer")
        w.writerow(header)
        rows = crawlmxds(folder)
        w.writerows(rows)

def crawlmxds(folder):
    for root, dirs, files in os.walk(folder):
        for f in files:
            if f.lower().endswith(".mxd"):
                mxdName = os.path.splitext(f)[0]
                mxdPath = os.path.join(root, f)
                mxd = arcpy.mapping.MapDocument(mxdPath)
                print mxdPath
                print mxdName
                for lyr in arcpy.mapping.ListLayers(mxd):
                    if lyr.supports("DATASOURCE"):
                        dSource = str(lyr.dataSource)
                        if '.sde' in dSource:
                            fcName = dSource.split('.sde\\')
                            print "Layer Name: " + lyr.name
                            print "Feature Class: " + fcName[1]
                        elif '.gdb' in dSource:
                            fcName = dSource.split('.gdb\\')
                            print "Layer Name: " + lyr.name
                            print "Feature Class: " + fcName[1]
##                        if lyr.supports("definitionQuery"):
##                            dq = lyr.definitionQuery
##                            print "Definition Query: " + lyr.definitionQuery
##                        else:
##                            print "NA"
                        if lyr.supports("SERVICEPROPERTIES"):
                            if lyr.serviceProperties["ServiceType"] != "SDE":
                                print "Service Type:  " + lyr.serviceProperties.get('ServiceType', 'N/A')
                                print "     URL:          " + lyr.serviceProperties.get('URL', 'N/A')
                                print "     Connection:   " + lyr.serviceProperties.get('Connection','N/A')
                                print "     Server:       " + lyr.serviceProperties.get('Server','N/A')
                                print "     Cache:        " + lyr.serviceProperties.get('Cache','N/A')
                                print "     User Name:    " + lyr.serviceProperties.get('UserName','N/A')
                                print "     Password:     " + lyr.serviceProperties.get('Password','N/A')
                                print ""
                            else:
                                print "Service Type:  " + lyr.serviceProperties.get('Service Type', 'N/A')
                                db = lyr.serviceProperties.get('Database', 'N/A')
                                print "     Database:       " + lyr.serviceProperties.get('Database', 'N/A')
                                svr = lyr.serviceProperties.get('Server', 'N/A')
                                print "     Server:         " + lyr.serviceProperties.get('Server', 'N/A')
                                srvc = lyr.serviceProperties.get('Service', 'N/A')
                                print "     Service:        " + lyr.serviceProperties.get('Service', 'N/A')
                                ver = lyr.serviceProperties.get('Version', 'N/A')
                                print "     Version:        " + lyr.serviceProperties.get('Version', 'N/A')
                                un = lyr.serviceProperties.get('UserName', 'N/A')
                                print "     User Name:      " + lyr.serviceProperties.get('UserName', 'N/A')
                                auth = lyr.serviceProperties.get('Authentication', 'N/A')
                                print "     Authentication: " + lyr.serviceProperties.get('Authentication', 'N/A')
                                print ""

                        seq = (mxdName, lyr.name, fcName[1], db, svr, ver, un, auth, ags);
                        yield seq
                del mxd

if __name__ == "__main__":
    folderPath = r"mxdpath" # or arcpy.GetParameterAsText(0)
    output = r"csvpath" # or arcpy.GetParameterAsText(1)
    main(folderPath, output)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 21:01:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020693#M59661</guid>
      <dc:creator>StevenCross</dc:creator>
      <dc:date>2021-01-27T21:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: UnboundLocalError  using sequence</title>
      <link>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020703#M59662</link>
      <description>&lt;P&gt;Just glanced through, but you've not created the variables (db, svr...) to yield when the condition is met (not SDE) - only when SDE.&amp;nbsp; A quick fix might be to assign them within that if true block - db = "None" ... etc.&lt;/P&gt;&lt;P&gt;Far from a coding whizz so take a big pinch of salt with that.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 21:12:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020703#M59662</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-01-27T21:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: UnboundLocalError  using sequence</title>
      <link>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020730#M59663</link>
      <description>&lt;P&gt;the variable/property "db" only gets a value in one case.&lt;/P&gt;&lt;P&gt;A safe way of finding out what is going on is to assign potential returns to some value, whether it be None prior to running any loops.&amp;nbsp; For example&lt;/P&gt;&lt;P&gt;db = None; svr =None;&amp;nbsp; ver = None; un = None; auth = None;&amp;nbsp; ags = None&lt;/P&gt;&lt;LI-CODE lang="python"&gt;db = None; svr =None;  ver = None; un = None; auth = None;  ags = None

def crawlmxds(seq):
    for i in seq:
        print("{}".format(i))
        

seq = [db, svr, ver, un, auth, ags]

crawlmxds(seq)
None
None
None
None
None
None&lt;/LI-CODE&gt;&lt;P&gt;Then when you see a None, you know something is wrong.&lt;/P&gt;&lt;P&gt;The alternative is check each variable assignment before you try to use it, as is your current case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 21:45:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020730#M59663</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-01-27T21:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: UnboundLocalError  using sequence</title>
      <link>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020819#M59666</link>
      <description>&lt;P&gt;Thanks Dan and David! I took a look at both of your suggestions and what think I may have found a solution...however not the prettiest or most understandable but if I put the variables after the Service Type variable like below it works.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if lyr.supports("SERVICEPROPERTIES"):
                            if lyr.serviceProperties["ServiceType"] != "SDE":
                            db = lyr.serviceProperties.get('Database', 'N/A')
                            print "     Database:       " + lyr.serviceProperties.get('Database', 'N/A')
                            svr = lyr.serviceProperties.get('Server', 'N/A')
                            print "     Server:         " + lyr.serviceProperties.get('Server', 'N/A')
                            srvc = lyr.serviceProperties.get('Service', 'N/A')
                            print "     Service:        " + lyr.serviceProperties.get('Service', 'N/A')
                            ver = lyr.serviceProperties.get('Version', 'N/A')
                            print "     Version:        " + lyr.serviceProperties.get('Version', 'N/A')
                            un = lyr.serviceProperties.get('UserName', 'N/A')
                            print "     User Name:      " + lyr.serviceProperties.get('UserName', 'N/A')
                            auth = lyr.serviceProperties.get('Authentication', 'N/A')
                                print "     Authentication: " + lyr.serviceProperties.get('Authentication', 'N/A')
                                print "Service Type:  " + lyr.serviceProperties.get('ServiceType', 'N/A')
                                print "     URL:          " + lyr.serviceProperties.get('URL', 'N/A')
                                print "     Connection:   " + lyr.serviceProperties.get('Connection','N/A')
                                print "     Server:       " + lyr.serviceProperties.get('Server','N/A')
                                print "     Cache:        " + lyr.serviceProperties.get('Cache','N/A')
                                print "     User Name:    " + lyr.serviceProperties.get('UserName','N/A')
                                print "     Password:     " + lyr.serviceProperties.get('Password','N/A')
                                print ""
                            else:
                                print ""
                        seq = (mxdName, lyr.name, fcName[1], db, svr, ver, un, auth, ags);
                        yield seq&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 27 Jan 2021 23:53:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unboundlocalerror-using-sequence/m-p/1020819#M59666</guid>
      <dc:creator>StevenCross</dc:creator>
      <dc:date>2021-01-27T23:53:55Z</dc:date>
    </item>
  </channel>
</rss>

