My goal is to copy a dataset from arcsde to a file gdb. I have created a gdb with the name of current date of the system just to maintain the archive of gdbs. I am trying to copy the dataset with specified name and copying that to the newly created gdb. My code is as follows which giving me the error pasted at the bottom:
>>> today=date.today()
>>> d4 = today.strftime("%b_%d_%Y")
>>> arcpy.CreateFileGDB_management("D:\Data",'{}'.format(d4))
>>> outpath = r'D:\Data\{}'.format(d4) + ".gdb"
>>> list=arcpy.ListDatasets()
>>> for b in list:
... if b == 'CONSDATA.NC_COM':
... output = outpath + os.sep + b
... arcpy.CopyFeatures_management(b,output)
Runtime error Traceback (most recent call last): File "<string>", line 4, in <module> File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 2574, in CopyFeatures raise e ExecuteError: ERROR 000732: Input Features: Dataset CONSDATA.NC_COM does not exist or is not supported.
Solved! Go to Solution.
My bad! To do to the dataset, you need to create one first and then add it in the output path.
edited to add a check for the existence of the dataset.
for b in list:
if b == 'CONSDATA.NC_COM':
# check if the dataset already exists and create it if it does not.
output_ds = os.path.join(outpath, ds)
if not arcpy.Exists(output_ds):
# get the spatial reference of the old one to use in the new one
spatial_ref = arcpy.Describe(b).spatialReference
# create new ds
arcpy.management.CreateFeatureDataset(outpath, b, spatial_ref)
# transfer the fcs.
for fc in arcpy.ListFeatureClasses(feature_dataset=b):
fc_path = os.path.join(arcpy.env.workspace, b, fc)
output = os.path.join(output_ds, fc)
arcpy.CopyFeatures_management(fc_path, output)
If that is the only dataset that you want, you can skip the list and list iteration and create a more targeted script using
ds = arcpy.ListDatasets('CONSDATA.NC_COM')[0]
You need to iterate over the featureclasses in that dataset:
for b in list:
if b == 'CONSDATA.NC_COM':
for fc in arcpy.ListFeatureClasses(feature_dataset=b):
fc_path = os.path.join(arcpy.env.workspace, b, fc)
output = outpath + os.sep + fc
arcpy.CopyFeatures_management(fc_path, output)
Hi jeffK, thanks for the answer. It did work however it is copying the feature classes in the GDB without the dataset. Is there any way to copy the complete dataset with the feature classes within that?
My bad! To do to the dataset, you need to create one first and then add it in the output path.
edited to add a check for the existence of the dataset.
for b in list:
if b == 'CONSDATA.NC_COM':
# check if the dataset already exists and create it if it does not.
output_ds = os.path.join(outpath, ds)
if not arcpy.Exists(output_ds):
# get the spatial reference of the old one to use in the new one
spatial_ref = arcpy.Describe(b).spatialReference
# create new ds
arcpy.management.CreateFeatureDataset(outpath, b, spatial_ref)
# transfer the fcs.
for fc in arcpy.ListFeatureClasses(feature_dataset=b):
fc_path = os.path.join(arcpy.env.workspace, b, fc)
output = os.path.join(output_ds, fc)
arcpy.CopyFeatures_management(fc_path, output)
If that is the only dataset that you want, you can skip the list and list iteration and create a more targeted script using
ds = arcpy.ListDatasets('CONSDATA.NC_COM')[0]
Yes! I tried with the similar logic. Thanks a lot. 🙂