Using a list with arcpy.env.workspace

482
8
Jump to solution
02-08-2024 02:06 PM
TychoGranville
Occasional Contributor II

I am trying to copy a geodb to a different name, do work on the copy, then overwrite the original. My error checking fails before it can even start.

My error checking is to make sure the projection is correct before continuing on that (copied) FC. What I'm running into is changing arcpy.env.workspace "on the fly" to the newly created geodb; it can't find the FC's to check the projection.

I've narrowed the snippet below to just show the error generated (and only one list item), not the work I'm trying to do. I have tried all manner of things trying to put a string together for the "on the fly" part.

Python 2 because that's all they have.

Spoiler
import arcpy
### Lists
geodb_list = [r"S:\GIS\Scratch\Conversion2\GISPWBASE.gdb"]
# Note that 2nd file exists: "S:\GIS\Scratch\Conversion2\GISPWBASE.gdbsp.gdb"

for geodb in geodb_list:
 env = str(geodb)
 ext = "sp.gdb"
 geodb2 = str(env+ext)
 arcpy.env.workspace = env
 feature_classes = arcpy.ListFeatureClasses()
 print feature_classes
 for fc in feature_classes:
  spatial_ref = arcpy.Describe(fc).spatialReference
  print (spatial_ref.name)
 #
 arcpy.env.workspace = geodb2
 feature_classes = arcpy.ListFeatureClasses()
 print feature_classes
 for fc in feature_classes:
  spatial_ref = arcpy.Describe(fc).spatialReference
  print (spatial_ref.name)
  
  #OUTPUT
  [u'Locates', u'Easements', u'NWGASMAPS', u'NWNaturalGas', u'namedstreams', u'lakes_rivers', u'LocatesAnno', u'addresses', u'Taxlots', u'Roads', u'TheDallesIrrigation']
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
NAD_1927_StatePlane_Oregon_North_FIPS_3601
[u'Locates', u'Easements', u'NWGASMAPS', u'NWNaturalGas', u'namedstreams', u'lakes_rivers', u'LocatesAnno', u'addresses', u'Taxlots', u'Roads', u'TheDallesIrrigation']
Traceback (most recent call last):
  File "<module1>", line 37, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.7\arcpy\arcpy\__init__.py", line 1260, in Describe
    return gp.describe(value, data_type)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.7\arcpy\arcpy\geoprocessing\_base.py", line 376, in describe
    self._gp.Describe(*gp_fixargs(args, True)))
IOError: "Locates" does not exist

Thanks,

Tycho

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffWard
Occasional Contributor III

What if you used an underscore instead of . for your 2nd geodatabase? like:

geodb2 = env.replace('.', '_') + ext
Jeff Ward
Summit County, Utah

View solution in original post

8 Replies
AlfredBaldenweck
MVP Regular Contributor

Does it work if you just join the path of the featureclass to geodb?

 for fc in feature_classes:
  spatial_ref = arcpy.Describe(os.path.join(geodb,fc)).spatialReference
  print (spatial_ref.name)
TychoGranville
Occasional Contributor II

Unfortunately that adds a / to the path and still gives me pretty much the same error ("cannot find" vs "does not exist"):

for fc in feature_classes:
spatial_ref = arcpy.Describe(os.path.join(geodb2,fc)).spatialReference
print (spatial_ref.name)

IOError: "S:\GIS\Scratch\Conversion2\GISPWBASE.gdbsp.gdb\Locates" does not exist

(I need to use geodb2 as that is the variable that is failing)

Part of what I don't understand is both versions of the arcpy.env.workspace can find the feature classes with the same printed output:

 [u'Locates', u'Easements', u'NWGASMAPS', u'NWNaturalGas', u'namedstreams', u'lakes_rivers', u'LocatesAnno', u'addresses', u'Taxlots', u'Roads', u'TheDallesIrrigation']

but the hacked together one can't find the FCs for the next step.

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Another dumb question but I have to ask: does geodb2 actually exist?

Right now it looks like you're naming it but not creating it.

0 Kudos
TychoGranville
Occasional Contributor II

Yep, it's there. I took out the copy-the-geodatabase part (which works just fine) to narrow down the issue and save time while I run this over and over (and over...)

0 Kudos
JeffWard
Occasional Contributor III

What if you used an underscore instead of . for your 2nd geodatabase? like:

geodb2 = env.replace('.', '_') + ext
Jeff Ward
Summit County, Utah
TychoGranville
Occasional Contributor II

WOOT! It worked!! I've been banging my head on this for an embarrassing amount of time.

I'm so used to seeing files that have extra dots in the name before the extension this would never have occurred to me. Need to revert back to the old DOS days of only a dot before the the extension, no special characters, no spaces. Not sure I can manage 8 character names though...

JeffWard
Occasional Contributor III

I'm glad it worked for you. I run in fear from spaces in file names and folder names. I am mocked by my younger colleagues. 🙂

Jeff Ward
Summit County, Utah
AlfredBaldenweck
MVP Regular Contributor

The weird thing is I manually made a file gdb that looked just like his and it worked just fine in the python window?

example.gdbsp.gdb

Unless this is only an issue in Python 2.7?

0 Kudos