I am looking for the way to speed up my script. The final output is as required, but it's clunky and really slow. I know it's slow because of the double for..in.. loops. So, how could I make it a bit smoother?
My script updates existing feature classes. I have two folders where all up-to-date shapefiles (shp) are stored. My script checks what shps are in each folder, and then searches for the same named featureclasses (these are older versions) in 4 geodatabases. So shp has to go to the gdb that already contains featureclass with the same name. I hope it is simple enough. Just to give you some idea, it takes over 1h to run this script, I have about 45 shapefiles (~1gb), these are copied over to 4 geodatabases.
Is there any way to avoid these time consuming double for..in.. loops? I need to mention that folders are stored on different server than gdb's, and that is a main reason of having it so slow. When I tested the script using only C drive it took 6-8minutes!
import time
import arcpy
startTime = time.clock()
print "Start"
FCProducts = r"C:\Python\DataConversion\shapefiles"
GBProducts = r"C:\Python\DataConversion\shapefile_gp"
adminBdry = r"C:\Python\DataConversion\temp1.gdb"
GnR = r"C:\DataConversion\temp2.gdb"
Invenories = r"C:\Python\DataConversion\temp3.gdb"
NFE = r"C:\Python\DataConversion\temp4.gdb"
arcpy.env.overwriteOutput = True
arcpy.env.workspace = FCProducts
listFC = [0]
listFC = arcpy.ListFeatureClasses()
del arcpy.env.workspace
for product in listFC:
f2fOutput = product[:-4] + "_new"
arcpy.env.workspace = GnR
listGnrFC = arcpy.ListFeatureClasses()
for gnrFC in listGnrFC:
if gnrFC == product[:-4]:
arcpy.FeatureClassToFeatureClass_conversion(product[:-4], GnR, f2fOutput)
print product, " copied to ", GnR
arcpy.Delete_management(gnrFC)
print "old ", gnrFC, " deleted"
arcpy.Rename_management(f2fOutput, product[:-4])
print "output renamed", "\n"
arcpy.env.workspace = adminBdry
listAdBdryFC = arcpy.ListFeatureClasses()
for AdBdry in listAdBdryFC:
if AdBdry == product[:-4]:
arcpy.FeatureClassToFeatureClass_conversion(product[:-4], adminBdry, f2fOutput)
print product, " copied to ", adminBdry
arcpy.Delete_management(AdBdry)
print "old ", AdBdry, " deleted"
arcpy.Rename_management(f2fOutput, product[:-4])
print "output renamed", "\n"
arcpy.env.workspace = Invenories
listInventFC = arcpy.ListFeatureClasses()
for invent in listInventFC:
if invent == product[:-4]:
arcpy.FeatureClassToFeatureClass_conversion(product[:-4], Invenories, f2fOutput)
print product, " copied to ", Invenories
arcpy.Delete_management(invent)
print "old ", invent, " deleted"
arcpy.Rename_management(f2fOutput, product[:-4])
print "output renamed", "\n"
arcpy.env.workspace = NFE
listNfeFC = arcpy.ListFeatureClasses()
for nfe in listNfeFC:
if nfe == product[:-4]:
arcpy.FeatureClassToFeatureClass_conversion(product[:-4], NFE, f2fOutput)
print product, " copied to ", NFE
arcpy.Delete_management(nfe)
print "old ", nfe, " deleted"
arcpy.Rename_management(f2fOutput, product[:-4])
print "output renamed", "\n"
arcpy.env.workspace = GBProducts
listFCgb = [0]
listFCgb = arcpy.ListFeatureClasses()
for productGB in listFCgb:
f2fOutputGB = productGB[:-4] + "_new"
arcpy.env.workspace = Invenories
listInventGB = arcpy.ListFeatureClasses()
for InventGB in listInventGB:
if InventGB == productGB[:-4]:
arcpy.FeatureClassToFeatureClass_conversion(productGB[:-4], Invenories, f2fOutputGB)
print productGB, " copied to ", Invenories
arcpy.Delete_management(InventGB)
print "old ", InventGB, " deleted"
arcpy.Rename_management(f2fOutputGB, productGB[:-4])
print "output renamed", "\n"
arcpy.env.workspace = NFE
listNfeGB = arcpy.ListFeatureClasses()
for nfeGB in listNfeGB:
if nfeGB == productGB[:-4]:
arcpy.FeatureClassToFeatureClass_conversion(productGB[:-4], NFE, f2fOutputGB)
print productGB, " copied to ", NFE
arcpy.Delete_management(nfeGB)
print "old ", nfeGB, " deleted"
arcpy.Rename_management(f2fOutputGB, productGB[:-4])
print "output renamed", "\n"
print "completed within:", time.clock() - startTime, "seconds", "\n"
I am using Windows XP, ArcGis 10.0, Python 2.6.6