Select to view content in your preferred language

Python 3 Script Not Executing without Error in ArcGIS Pro 3.1.2 but runs Error Free in ArcMap 10.8.1

3000
10
Jump to solution
05-29-2023 02:40 PM
Zedwards_SIPI
New Contributor III

Hello Folks,

I am not a programmer but am getting more and more familiar with ArcPy with ArcGIS Pro these days. I have purchased a digital book that has proven quite useful too. That being said, I am receiving the following error when executing a toolbox script (.py): 

Again, it works fine when run from ArcMap or ArcCatalog and even does what it's supposed to do in Pro but throws the error below .I have also updated my project packages but do get errors when trying to do certain updates or install Spyder from there. Any ideas are much appreciated.

Start Time: Monday, May 29, 2023 3:30:17 PM
NM_wind
ShapeFile
1328
Nothing to Delete
Test
FeatureClass
0
EMPTY VECTOR DATASET DELETED
Traceback (most recent call last):
  File "C:\Users\zach.edwards\OneDrive - Western Data Systems\Documents\AllTerraCentral\Python\ListFCFeaturesRecursivelyCountNameReportDeletezero_PRO.py", line 33, in <module>
    if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 23607, in GetCount
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 23604, in GetCount
    retval = convertArcObjectToPythonObject(gp.GetCount_management(*gp_fixargs((in_rows,), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.ERROR 000840: The value is not a Table View.ERROR 000840: The value is not a Raster Layer.
Failed to execute (GetCount) 

The actual .py is pasted here below...

import arcpy
import os
 
workspace = arcpy.GetParameterAsText(0)
outFile = open(r"C:\temp\VectorDataList.txt", "a")
shapefiles = []
 
walk = arcpy.da.Walk(workspace, topdown=True, datatype="FeatureClass")
 
for dirpath, dirnames, filenames in walk:
for filename in filenames:
shapefiles.append(os.path.join(dirpath, filename))
for shapefile in shapefiles:
layerName = arcpy.Describe(shapefile).baseName
layerPath = arcpy.Describe(shapefile).path
layerDType = arcpy.Describe(shapefile).dataType
arcpy.AddMessage(layerName)
arcpy.AddMessage(layerDType)
arcpy.MakeFeatureLayer_management (shapefile, layerName)
arcpy.AddMessage(arcpy.GetCount_management(layerName).getOutput(0))
if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:
arcpy.Delete_management(shapefile)
arcpy.AddWarning("EMPTY VECTOR DATASET DELETED")
else:
arcpy.AddWarning("Nothing to Delete")
outFile.write("\n" "Filename: ")
outFile.write(layerName)
outFile.write("\n""Path: ")
outFile.write(dirpath)
outFile.write("\n" "DataType: ")
outFile.write(layerDType)
outFile.write("\n" "Feature Count: ")
if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:
outFile.write(arcpy.GetCount_management(layerName).getOutput(0))
outFile.write("\n" "EMPTY VECTOR DATASET DELETED")
else:
outFile.write(arcpy.GetCount_management(layerName).getOutput(0))
outFile.write("\n")
outFile.close()
 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Adding to Dan's advice, print the path out of that file that it is stopping on.

I tried your code on a folder and the arcpy.da.Walk did not honor the FeatureClass datatype and included RasterBand files too, which caused it to fail. You can add some logic to your code to skip these unaccounted file types that sneak through so you can check them out manually but still work through the Featureclasses and shapefiles:

import arcpy
import os

workspace = arcpy.GetParameterAsText(0)
if workspace is None or workspace == '':
    workspace = r'default path to parent folder for running in IDE'


def outfile_writer(layerName, layerPath, lyr_view, msg):
    '''
    helper function for writing the output to file
    '''
    outFile.write(f"\nFilename: {layerName} "
                  f"\nPath: {layerPath} "
                  f"\nFeature Count: {arcpy.management.GetCount(lyr_view).getOutput(0)}"
                  f"\n{msg}\n")

shapefiles = []

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, topdown=True, datatype="FeatureClass"):
    for filename in filenames:
        shapefiles.append(os.path.join(dirpath, filename))

with open(r"path to \VectorDataList.txt", "a") as outFile:
    for shapefile in shapefiles:
        desc = arcpy.Describe(shapefile)
        layerName = desc.baseName
        layerPath = desc.path
        layerDType = desc.dataType

        arcpy.AddMessage(layerName)
        arcpy.AddMessage(layerDType)
        arcpy.AddMessage(layerPath)

        if layerDType in ['FeatureClass', 'ShapeFile']:
            lyr_view = arcpy.MakeFeatureLayer_management (shapefile, f'{layerName}_lyr')
            try:
                arcpy.AddMessage(arcpy.management.GetCount(lyr_view).getOutput(0))
                if int(arcpy.management.GetCount(lyr_view).getOutput(0)) == 0:
                    arcpy.AddMessage("EMPTY VECTOR DATASET DELETED")
                    outfile_writer(layerName, layerPath, lyr_view, 'EMPTY VECTOR DATASET DELETED')
                    arcpy.management.Delete(shapefile)
                else:
                    arcpy.AddMessage("Nothing to Delete")
                    outfile_writer(layerName, layerPath, lyr_view, 'DATASET NOT DELETED')

            except Exception as ex:
                arcpy.AddMessage(f"Could not open {layerName} at {layerPath}")
                outFile.write(f"\nCOULD NOT DELETE! {layerName} at {layerDType}")

        else:
            outFile.write(f"\nSKIPPIED! arcpy.da.Walk datatype filter failed: {layerName} is {layerDType}\n")
            arcpy.AddMessage(f"\nSKIPPIED! arcpy.da.Walk datatype filter failed: {layerName} is {layerDType}")

 

View solution in original post

10 Replies
Zedwards_SIPI
New Contributor III

One more thing, I did run the following tool to see if there were any issues and none were reported both on the toolbox and .py file.

0 Kudos
DanPatterson
MVP Esteemed Contributor

formatting will provide proper indentation and line numbers to make it easier to read your code

Code formatting ... the Community Version - Esri Community


... sort of retired...
0 Kudos
Zedwards_SIPI
New Contributor III
import arcpy
import os

workspace = arcpy.GetParameterAsText(0)
outFile = open(r"C:\temp\VectorDataList.txt", "a")
shapefiles = []

walk = arcpy.da.Walk(workspace, topdown=True, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
	for filename in filenames:
		shapefiles.append(os.path.join(dirpath, filename))
for shapefile in shapefiles:
	layerName = arcpy.Describe(shapefile).baseName
	layerPath = arcpy.Describe(shapefile).path
	layerDType = arcpy.Describe(shapefile).dataType
	arcpy.AddMessage(layerName)
	arcpy.AddMessage(layerDType)
	arcpy.MakeFeatureLayer_management (shapefile, layerName)
	arcpy.AddMessage(arcpy.management.GetCount(layerName).getOutput(0))
	if int(arcpy.management.GetCount(layerName).getOutput(0)) == 0:
		arcpy.management.Delete(shapefile)
		arcpy.AddWarning("EMPTY VECTOR DATASET DELETED")
	else:
		arcpy.AddWarning("Nothing to Delete")
	outFile.write("\n" "Filename: ")
	outFile.write(layerName)
	outFile.write("\n""Path: ")
	outFile.write(dirpath)
	outFile.write("\n" "DataType: ")
	outFile.write(layerDType)
	outFile.write("\n" "Feature Count: ")
	if int(arcpy.management.GetCount(layerName).getOutput(0)) == 0:
		outFile.write(arcpy.management.GetCount(layerName).getOutput(0))
		outFile.write("\n" "EMPTY VECTOR DATASET DELETED")
	else:
		outFile.write(arcpy.management.GetCount(layerName).getOutput(0))
	outFile.write("\n")
outFile.close()

Thanks Dan. New to this. 

0 Kudos
by Anonymous User
Not applicable

line 33-35 code seems like it's redundant since it is done in line 21-23. Put line 34 in the 21 if and line 37 in the else on 24.

You can also just get the describe once:

desc = arcpy.Describe(shapefile)
layerName = desc.baseName
layerPath = desc.path
layerDType = desc.dataType

The memory workspace is a bit of mystery with its functionality and I am wondering if the layer you create is also removed when you delete it in 22 when the code reaches 33, causing that error.  I'd avoid using the same name for the layer in memory as the layer referenced in the loop, even though the shapefile is a path. layerName starts as a string variable, but then it becomes a reference to a layer view. Outside of the esri environment, this may be causing problems.  Try to be more explicit in the variables like this:

for shapefile in shapefiles:
    desc = arcpy.Describe(shapefile)
    layerName = desc.baseName
    layerPath = desc.path
    layerDType = desc.dataType
    arcpy.AddMessage(layerName)
    arcpy.AddMessage(layerDType)
    lyr_view = arcpy.MakeFeatureLayer_management (shapefile, f'{layerName}_lyr')
    arcpy.AddMessage(arcpy.management.GetCount(lyr_view).getOutput(0))
    ...

 

Zedwards_SIPI
New Contributor III

Thansk Jeff. All working now. The difference is good code etiquette versus poor. I cleaned it up and it's working well. I just need to reorganize.  

0 Kudos
Zedwards_SIPI
New Contributor III

What's odd is that everything works error free with the code below but when I have many subfolders (drill deeper into file explorer), I get the following messages/errors: 

 

 

import arcpy
import os

workspace = arcpy.GetParameterAsText(0)
outFile = open(r"C:\temp\VectorDataList.txt", "a")
shapefiles = []

walk = arcpy.da.Walk(workspace, topdown=True, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        shapefiles.append(os.path.join(dirpath, filename))
for shapefile in shapefiles:
        desc = arcpy.Describe(shapefile)
        layerName = desc.baseName
        layerPath = desc.path
        layerDType = desc.dataType
        arcpy.AddMessage(layerName)
        arcpy.AddMessage(layerDType)
#       arcpy.MakeFeatureLayer_management (shapefile, layerName)
#       arcpy.AddMessage(arcpy.management.GetCount(layerName).getOutput(0))
        lyr_view = arcpy.MakeFeatureLayer_management (shapefile, f'{layerName}_lyr')
        arcpy.AddMessage(arcpy.management.GetCount(lyr_view).getOutput(0))
        if int(arcpy.management.GetCount(lyr_view).getOutput(0)) == 0:
            arcpy.AddMessage("EMPTY VECTOR DATASET DELETED")
            outFile.write("\n" "Filename: ")
            outFile.write(layerName)
            outFile.write("\n""Path: ")
            outFile.write(dirpath)
            outFile.write("\n" "DataType: ")
            outFile.write(layerDType)
            outFile.write("\n" "Feature Count: ")
            outFile.write(arcpy.management.GetCount(lyr_view).getOutput(0))
            outFile.write("\n" "EMPTY VECTOR DATASET DELETED")
            arcpy.management.Delete(shapefile)
            outFile.write("\n")
        else:
            arcpy.AddMessage("Nothing to Delete")
            outFile.write("\n" "Filename: ")
            outFile.write(layerName)
            outFile.write("\n""Path: ")
            outFile.write(dirpath)
            outFile.write("\n" "DataType: ")
            outFile.write(layerDType)
            outFile.write("\n" "Feature Count: ")
            outFile.write(arcpy.management.GetCount(lyr_view).getOutput(0))
            outFile.write("\n")
outFile.close()

 

Start Time: Tuesday, May 30, 2023 11:03:15 AM
Polylines
FeatureClass
Traceback (most recent call last):
  File "C:\Users\zach.edwards\OneDrive - Western Data Systems\Documents\AllTerraCentral\Python\ListFCFeaturesRecursivelyCountNameReportDeletezero_PRO.py", line 22, in <module>
    lyr_view = arcpy.MakeFeatureLayer_management (shapefile, f'{layerName}_lyr')
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10316, in MakeFeatureLayer
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10313, in MakeFeatureLayer
    retval = convertArcObjectToPythonObject(gp.MakeFeatureLayer_management(*gp_fixargs((in_features, out_layer, where_clause, workspace, field_info), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
Failed to execute (MakeFeatureLayer).

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

paths... sometimes spaces, punctuation and OneDrive cause issues. Avoid their construction and use to avoid the "unexpected " error..


... sort of retired...
by Anonymous User
Not applicable

Adding to Dan's advice, print the path out of that file that it is stopping on.

I tried your code on a folder and the arcpy.da.Walk did not honor the FeatureClass datatype and included RasterBand files too, which caused it to fail. You can add some logic to your code to skip these unaccounted file types that sneak through so you can check them out manually but still work through the Featureclasses and shapefiles:

import arcpy
import os

workspace = arcpy.GetParameterAsText(0)
if workspace is None or workspace == '':
    workspace = r'default path to parent folder for running in IDE'


def outfile_writer(layerName, layerPath, lyr_view, msg):
    '''
    helper function for writing the output to file
    '''
    outFile.write(f"\nFilename: {layerName} "
                  f"\nPath: {layerPath} "
                  f"\nFeature Count: {arcpy.management.GetCount(lyr_view).getOutput(0)}"
                  f"\n{msg}\n")

shapefiles = []

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, topdown=True, datatype="FeatureClass"):
    for filename in filenames:
        shapefiles.append(os.path.join(dirpath, filename))

with open(r"path to \VectorDataList.txt", "a") as outFile:
    for shapefile in shapefiles:
        desc = arcpy.Describe(shapefile)
        layerName = desc.baseName
        layerPath = desc.path
        layerDType = desc.dataType

        arcpy.AddMessage(layerName)
        arcpy.AddMessage(layerDType)
        arcpy.AddMessage(layerPath)

        if layerDType in ['FeatureClass', 'ShapeFile']:
            lyr_view = arcpy.MakeFeatureLayer_management (shapefile, f'{layerName}_lyr')
            try:
                arcpy.AddMessage(arcpy.management.GetCount(lyr_view).getOutput(0))
                if int(arcpy.management.GetCount(lyr_view).getOutput(0)) == 0:
                    arcpy.AddMessage("EMPTY VECTOR DATASET DELETED")
                    outfile_writer(layerName, layerPath, lyr_view, 'EMPTY VECTOR DATASET DELETED')
                    arcpy.management.Delete(shapefile)
                else:
                    arcpy.AddMessage("Nothing to Delete")
                    outfile_writer(layerName, layerPath, lyr_view, 'DATASET NOT DELETED')

            except Exception as ex:
                arcpy.AddMessage(f"Could not open {layerName} at {layerPath}")
                outFile.write(f"\nCOULD NOT DELETE! {layerName} at {layerDType}")

        else:
            outFile.write(f"\nSKIPPIED! arcpy.da.Walk datatype filter failed: {layerName} is {layerDType}\n")
            arcpy.AddMessage(f"\nSKIPPIED! arcpy.da.Walk datatype filter failed: {layerName} is {layerDType}")

 

Zedwards_SIPI
New Contributor III

Unfortunately, I am getting the same errors with this code. I will test a few things and report back. What's odd is that the path that I am testing is my desktop which is simply: C:\Users\zach.edwards\Desktop

0 Kudos