3

Im trying to create a script that will copy the polygon files from one gdb into a newly created gdb but the files always are generated into the default gdb. ive played around with the "outputs" of CopyFeatures but they always still are generated in the default gdb.

My script is below

import arcpy, os
from arcpy import env
env.overwriteOutput = True
env.workspace = "C:\EsriPress\Python\Data\Exercise06\Results\NM.gdb"
fclist = arcpy.ListFeatureClasses()
arcpy.CreateFileGDB_management("C:\EsriPress\Python\Data\Exercise06\Results", "test.gdb")
outputgdb = "C:\EsriPress\Python\Data\Exercise06\Results\test.gdb"
for fc in fclist:
    fcdesc = arcpy.Describe(fc)
    if fcdesc.shapeType == "Polygon":
        arcpy.CopyFeatures_management(fc,os.path.join(outputgdb, "polygon_"+fc))
    else:
        print fcdesc.basename + " is not a polygon"
3

I think your code is almost there, however there are some escape character issues associated with your use of the paths to your data. Perhaps try the following:

import arcpy
import os

from arcpy import env
env.overwriteOutput = True
env.workspace = "C:\\EsriPress\\Python\\Data\\Exercise06\\Results\\NM.gdb"
#env.workspace = r"C:\EsriPress\Python\Data\Exercise06\Results\NM.gdb", this is an alternative way to use single \'s in paths.
fclist = arcpy.ListFeatureClasses()
arcpy.CreateFileGDB_management("C:\\EsriPress\\Python\\Data\\Exercise06\\Results", "test.gdb")
outputgdb = "C:\\EsriPress\\Python\\Data\\Exercise06\\Results\\test.gdb"

for fc in fclist:
    fcdesc = arcpy.Describe(fc)
    if fcdesc.shapeType == "Polygon":
        arcpy.CopyFeatures_management(fc, os.path.join(outputgdb, "polygon_" + fc))
    else:
        print(fcdesc.basename + " is not a polygon")

See also this stackoverflow post concerning the use of raw string literals, or the "r" prefix on the above string variable declaration.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.