Select to view content in your preferred language

OSError: "[list of filenames]" does not exist

3346
4
07-28-2020 08:20 AM
jblng
by
Occasional Contributor

Hello,

Apologies in advance if this is a duplicate question. This question seems to be slightly similar, but I think mine is more basic. I am trying to run a simple for loop in jupyter notebooks to iterate through tables in a gdb with arcpy.AlterField_management and update non-required field names with the name of the table. However, when running this operation I get an OSError: "[list of filenames]" does not exist. I got the same error when running the script in an arcgis toolbox. My code and the error message are below.

I saw in this stack overflow question that it might be an issue with my arcpy site package, but I don't know how to rectify if that were the issue.

Thanks!

Jo

------
import arcpy, os, sys
from arcpy import env

arcpy.env.overwriteOutput = True

# Set the workspace directory - make sure it's the copied gdb and not the original, in case you mess up
arcpy.env.workspace = r"P:\AFG\GEO\Team\Projects\Shock_Vulnerability\NTL\NTL_201801_202001_AFG_proj_zonalst_copy.gdb"

# Get the list of the zonal stats tables to process
tables = arcpy.ListTables(wild_card="*AFG_proj_zonalst", table_type="ALL")
print (tables)

# iterate through the tables in the gdb and update field names with the file names
for table in tables:
      print ("processing: " + table)
      # print field names
      # iterate through field names in table
      for fname in [f.name for f in arcpy.ListFields(tables, wild_card="", field_type="ALL") if not f.required]:
            print(fname)
            arcpy.AlterField_management(table, f.name, f.name+"_%Name%")
            print(fname)
-------
['NTL_QN_202001_AFG_proj_zonalst', 'NTL_201912_AFG_proj_zonalst', 'NTL_201911_AFG_proj_zonalst', 
'NTL_201910_AFG_proj_zonalst', 'NTL_201909_AFG_proj_zonalst', 'NTL_201908_AFG_proj_zonalst', 
'NTL_201907_AFG_proj_zonalst', 'NTL_201906_AFG_proj_zonalst', 'NTL_201905_AFG_proj_zonalst', 
'NTL_201904_AFG_proj_zonalst', 'NTL_201903_AFG_proj_zonalst', 'NTL_201902_AFG_proj_zonalst', 
'NTL_201901_AFG_proj_zonalst', 'NTL_201812_AFG_proj_zonalst', 'NTL_201811_AFG_proj_zonalst', 
'NTL_201810_AFG_proj_zonalst', 'NTL_201809_AFG_proj_zonalst', 'NTL_201808_AFG_proj_zonalst', 
'NTL_201807_AFG_proj_zonalst', 'NTL_201804_AFG_proj_zonalst', 'NTL_201801_AFG_proj_zonalst', 
'NTL_201806_AFG_proj_zonalst', 'NTL_201802_AFG_proj_zonalst', 'NTL_201805_AFG_proj_zonalst', 
'NTL_201803_AFG_proj_zonalst', 'NTL_202001_AFG_proj_zonalst'] processing: NTL_QN_202001_AFG_proj_zonalst
---------------------------------------------------------------------------OSError             
Traceback (most recent call last)<ipython-input-14-596e9c744258> in <module>     
16     # print field names     
17     # iterate through field names in table
---> 18     for fname in [f.name for f in arcpy.ListFields(tables, wild_card="", field_type="ALL") if not f.required]:     
19             print(fname)     
20             arcpy.AlterField_management(table, f.name, f.name+"_%Name%")C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py in ListFields(dataset, wild_card, field_type)   
1131    
1132         * String:   Only field types of String are returned."""
-> 1133     return gp.listFields(dataset, wild_card, field_type)   
1134    
1135 @_gptooldoc(None, [[["FeatureLayer", "Table", "TableView", "Dataset", "FeatureDataset"], "", "", ""]])C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py in listFields(self, *args)    
346         from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject    
347         return convertArcObjectToPythonObject(
--> 348                     self._gp.ListFields(*gp_fixargs(args, True)))    
349     def listIndexes(self, *args):    
350         """GP function ListIndexes"""
OSError: "['NTL_QN_202001_AFG_proj_zonalst', 'NTL_201912_AFG_proj_zonalst', 
'NTL_201911_AFG_proj_zonalst', 'NTL_201910_AFG_proj_zonalst', 'NTL_201909_AFG_proj_zonalst', 'NTL_201908_AFG_proj_zonalst',
'NTL_201907_AFG_proj_zonalst', 'NTL_201906_AFG_proj_zonalst', 'NTL_201905_AFG_proj_zonalst', 'NTL_201904_AFG_proj_zonalst', 
'NTL_201903_AFG_proj_zonalst', 'NTL_201902_AFG_proj_zonalst', 'NTL_201901_AFG_proj_zonalst', 'NTL_201812_AFG_proj_zonalst', 
'NTL_201811_AFG_proj_zonalst', 'NTL_201810_AFG_proj_zonalst', 'NTL_201809_AFG_proj_zonalst', 'NTL_201808_AFG_proj_zonalst', 
'NTL_201807_AFG_proj_zonalst', 'NTL_201804_AFG_proj_zonalst', 'NTL_201801_AFG_proj_zonalst', 'NTL_201806_AFG_proj_zonalst', 
'NTL_201802_AFG_proj_zonalst', 'NTL_201805_AFG_proj_zonalst', 'NTL_201803_AFG_proj_zonalst', 'NTL_202001_AFG_proj_zonalst']" does not exist
0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

I would start by passing the table from the loop and not the list of tables to ListFields, i.e., use the following line instead of yours:

for fname in [f.name for f in arcpy.ListFields(table, wild_card="", field_type="ALL") if not f.required]:
0 Kudos
jblng
by
Occasional Contributor

Hi Joshua, thanks for your quick response - It was indeed an issue with calling the list of tables instead of a single table. After updating the line, I no longer have the OSError, but am now having the following error: "NameError: name 'f' is not defined".

It was my understanding that 'f' is just the iterator variable for the list of fieldnames, and does not need to be defined before being called. Sorry for the really simple questions, I'm new to arcpy so it's probably another beginner error.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Your error is coming from this line:

 arcpy.AlterField_management(table, f.name, f.name+"_%Name%")

In Python3, comprehensions and generators have their own local namespaces, which prevents the bleeding of variable names from the structures into the calling namespace.  You can't call f.name because no variable named "f" has been created in either the local or global namespace.

Try:

arcpy.AlterField_management(table, fname, fname+"_%Name%")
jblng
by
Occasional Contributor

Hi Joshua, thank you. this makes sense and was helpful. Unfortunately I am now getting an OSerror again, that a file doesn't exist. It seems as though the for fname loop is not being iterated at all, as it is not printing anything. I think it could have to do with the fname variable again, but I can't  tell what aspect of it is.

My updated code is as follows: 

import arcpy, os, sys
from arcpy import env

arcpy.env.overwriteOutput = True

# Set the workspace directory - make sure it's the copied gdb and not the original, in case you mess up
arcpy.env.workspace = r"mygdb_.gdb"

# Get the list of the zonal stats tables to process
tables = arcpy.ListTables()
print(tables)

# iterate through the tables in the gdb and update field names with the file names
for table in tables: 
 print("processing: " + table)
 expression = str(table) # populates field
 # iterate through field names in the table and alter field name
 for fname in [f.name for f in arcpy.ListFields(table, wild_card="*AFG_proj_zonalst", field_type="ALL") if not f.required]:
    print(fname) 
    arcpy.AlterField_management(table, fname, fname+'"'+expression+'"')
    print(fname)
    with arcpy.da.UpdateCursor(table, [fname]) as uc:
       for row in uc:
       uc.updateRow([str(table)])
    del row, uc

and the error message: 

[prints list of files] 
processing: NTL_QN_202001_AFG_proj_zonalst 
processing: NTL_QN_202001_AFG_zonalst 
processing: NTL_QN_202001_proj_zonalst 
processing: NTL_QN_202001_zonalst 
processing: NTL_201912_AFG_proj_zonalst 
processing: NTL_201911_AFG_proj_zonalst 
processing: NTL_201910_AFG_proj_zonalst 
processing: NTL_201909_AFG_proj_zonalst 
processing: NTL_201908_AFG_proj_zonalst 
processing: NTL_201907_AFG_proj_zonalst 
processing: NTL_201906_AFG_proj_zonalst 
processing: NTL_201905_AFG_proj_zonalst 
processing: NTL_201904_AFG_proj_zonalst 
processing: NTL_201903_AFG_proj_zonalst 
processing: NTL_201902_AFG_proj_zonalst 
processing: NTL_201901_AFG_proj_zonalst
---------------------------------------------------------------------------OSError                                   
Traceback (most recent call last)<ipython-input-5-7d2495ac6c17> in <module>     
17     expression = str(table) # populates field     
18     # iterate through field names in the table and alter field name
---> 19     for fname in [f.name for f in arcpy.ListFields(table, wild_card="*AFG_proj_zonalst", field_type="ALL") if not f.required]:     
20         print(fname)     
21         arcpy.AlterField_management(table, fname, fname+'"'+expression+'"')C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py in ListFields(dataset, wild_card, field_type)   
1131    
1132         * String:   Only field types of String are returned."""
-> 1133     return gp.listFields(dataset, wild_card, field_type)   
1134    
1135 @_gptooldoc(None, [[["FeatureLayer", "Table", "TableView", "Dataset", "FeatureDataset"], "", "", ""]])C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py in listFields(self, *args)    
346         from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject    
347         return convertArcObjectToPythonObject(
--> 348                     self._gp.ListFields(*gp_fixargs(args, True)))    
349     def listIndexes(self, *args):    
350         """GP function ListIndexes"""
OSError: "NTL_201901_AFG_proj_zonalst" does not exist
0 Kudos