Hello, I'm trying to write a code to copy the feature classes inside of feature datasets.
I'm having two issues. I have 2 feature datasets, but when I do the "print(fcList)" , seems like only the features one dataset are being listed. The second one is that the tool stucks at the arcpy.CopyFeatures Here below my code:
Thanks in advance for any tip.
import arcpy
from arcpy import env
env.workspace = r'C:/Trials/NPD_FactMapsData_v3_0_2.gdb'
# Set local variables
OutputFGDB = r'C:/Trials/New File Geodatabase.gdb'
#env.workspace = arcpy.GetParameterAsText(0)
datasetList = arcpy.ListDatasets('*','Feature')
try:
for dataset in datasetList:
env.workspace = dataset
fcList = arcpy.ListFeatureClasses()
print (fcList)
for fc in fcList:
print('copying...')
arcpy.CopyFeatures_management(fc,OutputFGDB)
print("copy finished")
except:
# Report if there was an error
arcpy.AddError("Could not copy feature classes")
print ("Could not copy feature classes")
#print arcpy.GetMessages()
for dataset in datasetList:
add an indentation after this for the block, get rid of the try except block as this is likely failing with the lack of indent then excepting, add lots or print statements and review
Hello, thanks for your reply. I "cleaned" the code, but still not working. Still starts failing at fcList . Only the features from one dataset are being listed.For the second dataset fcList is returning "None". If I print dataset or datasetList, i can see the 2 datasets listed. Then in the arcpy.CopyFeatures its try to write a file with shapefile extension, of course an error.
import arcpy
arcpy.env.workspace = r"C:\Trials\NPD_FactMapsData_v3_0_2.gdb"
OutputFGDB = r"C:\Trials\New File Geodatabase.gdb"
datasetList = arcpy.ListDatasets()
for dataset in datasetList:
arcpy.env.workspace = dataset
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
arcpy.CopyFeatures_management(fc, OutputFGDB)
I think in CopyFeatures you would specify the path for the output FC rather than the destination GDB/Folder, grab the fc name and use that within your loop (something like os.path.join(OutputFGDB, fc_name). Have a look at the feature class class attributes and see the name property, may well be fc_name = fc.name, also ensure you import os module.
for fc in fcList: also has no indent on what you've copied.
Again put in some print statements and it would be helpful to see what output, messages you get also.
To get the script to iterate through your datasets you need to specify what datasets to use.
import arcpy
from arcpy import env
import os
env.workspace = "C:\\GDB.gdb"
outGDB = "C:\\outGDB.gdb"
env.overwriteOutput = True
dataSetList = arcpy.ListDatasets(feature_type='feature')
#check to make sure the list is populating
print dataSetList
for ds in dataSetList:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
#Check to see if the list is populating.
print fc
arcpy.CopyFeatures_management(fc, outGDB)
Another good options is to use Walk and allow it to collect the feature classes within your datasets.
Hello Mark,
Thank you, after your tips i figure it out the isse with listing all fcs. For the ouput I had to os.path.basename and splitext to get the fc names and then os.path.join to generate the full output.
Cheers,