I would like to check in one geodatabase and list the feature classes that are in it; check another geodatabsae for the list of feature classes, if they do not exist in the 2nd geodatabase then execute feature class to feature class. If one does then just skip. How can this be done with Python? Here is what I have so far. After I get my list how can I use arcpy.Exists
to check another .gdb from my list, and if an element in the list does appear in that .gdb then skip it, however if it does not, then execute the function?
>>> import arcpy
... from arcpy import env
... import os
...
... # Set the workspace for the ListFeatureClass function
... #
... env.workspace = r'C:\GIS_Data\MXDs\ServerMxdsR\working\gis_dev01.gdb'
...
... # Use the ListFeatureClasses function to return a list of
... # shapefiles.
... #
... fcList = arcpy.ListFeatureClasses()
-
To clarify: in gdb 1, list all FCs. If 1 FC exists in gdb 2 don't copy. If none of the FCS from gdb 1 exist in gdb 2 then copy?GISHuman– GISHuman2016年05月18日 18:57:01 +00:00Commented May 18, 2016 at 18:57
-
that is correctGeoffrey West– Geoffrey West2016年05月18日 18:58:40 +00:00Commented May 18, 2016 at 18:58
-
I didn't see these comments when I wrote my answer. My code copies any FC that is missing, but if you only want them copied if ALL are missing it'll need to be slightly different. I'll update my answerMidavalo– Midavalo ♦2016年05月18日 20:13:39 +00:00Commented May 18, 2016 at 20:13
-
2Just putting in my 2 cents here. But just blindly copying a feature class from one gdb to another may be problematic. One gdb might have a fc called "Roads" with 2 roads in it. The other might have one called "Roads" with 10,000 roads in it. Just looking at the name means it won't copy even though it has a more complete list of roads. This might be irrelevant for your workflow, but I just wanted to point out that you're not actually comparing data. Just feature class names.Fezter– Fezter2016年05月18日 22:32:19 +00:00Commented May 18, 2016 at 22:32
2 Answers 2
You want to try something like this
# Use the ListFeatureClasses function to return a list of
# shapefiles.
fcList = arcpy.ListFeatureClasses()
gdb2 = r'C:\GIS_Data\MXDs\ServerMxdsR\working\gis_dev02.gdb' # Your second geodatabase
for fc in fcList:
secondFC = os.path.join(gdb2, fc)
if arcpy.Exists(secondFC):
print "{} exists, not copying".format(fc)
else:
arcpy.FeatureClassToFeatureClass_conversion(fc, gdb2, fc) # Copies feature class from first gdb to second gdb
Loops through your feature classes from your fcList
, checks if they exist in your second gdb, and if they don't it performs arcpy.FeatureClassToFeatureClass_conversion
to copy from gdb 1 to gdb 2.
To only copy feature classes if none of them exist in gdb 2, try this:
# Use the ListFeatureClasses function to return a list of
# shapefiles.
fcList = arcpy.ListFeatureClasses()
gdb2 = r'C:\GIS_Data\MXDs\ServerMxdsR\working\gis_dev02.gdb' # Your second geodatabase
anyFCExists = 0
for fc in fcList:
secondFC = os.path.join(gdb2, fc)
if arcpy.Exists(secondFC):
anyFCExists = 1
break
if not anyFCExists:
for fc in fcList:
secondFC = os.path.join(gdb2, fc)
arcpy.FeatureClassToFeatureClass_conversion(fc, gdb2, fc) # Copies feature class from first gdb to second gdb
else:
print "One of the feature classes already exists, so not copying any"
This loops through all feature classes, and if it finds any already exist, then it sets the anyFCExists
flag. Then if the anyFCExists
flag is not set then it loops through the feature classes and copies them all across.
-
1so many arcpy functions that I don't know. arcpy.exists just helped a ton of my programs.Cody Brown– Cody Brown2019年07月19日 14:53:11 +00:00Commented Jul 19, 2019 at 14:53
Try this, using the while
loop will continue to loop until the condition is false. The code below will look for if the FC exists, if it doesn't, it will copy them all over.
import arcpy
import os
gdb1 = r"C:\Users\gb1.gdb"
gdb2 = r"C:\Users\New File Geodatabase.gdb"
walk = arcpy.da.Walk(gdb1,"FeatureClass")
walk2 = arcpy.da.Walk(gdb2,"FeatureClass")
#list all FC in gdb1
for dirpath, dirnames, filenames in walk:
for fc in filenames:
print fc
fc1 = os.path.join(gdb1,fc)
fc2 = os.path.join(gdb2,fc)
while arcpy.Exists(fc2):
print "FC exists in gdb2"
break
else:
print "Doesn't exist"
arcpy.FeatureClassToFeatureClass_conversion(fc1, gdb2, fc)
This one should work for finding one FC in the other GDB, if it exists, nothing will be copied:
import arcpy
import os
gdb1 = r"C:\Users\gdb1.gdb"
gdb2 = r"C:\Users\New File Geodatabase.gdb"
walk = arcpy.da.Walk(gdb1,"FeatureClass")
#list all FC in gdb1
c = 0
for dirpath, dirnames, filenames in walk:
for fc in filenames:
print fc
fc1 = os.path.join(gdb1,fc)
fc2 = os.path.join(gdb2,fc)
print c
if arcpy.Exists(fc2):
c=+1
print fc + "path does exist"
else:
print "does not exist"
if c<1:
arcpy.FeatureClassToFeatureClass_conversion(fc1, gdb2, fc)