2

I am trying to copy multiple shape files to a file geodatabase using python. I have been able to copy single .shp files to a geodatabase. Doing this one at a time is tedious and seems easier to do in ArcMap. I have enclosed the code I am using to do one at a time. How can I make this script work for all .shp files in the folder to be copied to a single geodatabase within that folder?

# Name: CopyFeatures_Example2.py
# Description: Convert all shapefiles in a folder to geodatabase feature 
# classes

# Import system modules
import arcpy
import os

# Set environment settings
arcpy.env.workspace = "C:/gisdata/roadData"

# Set local variables
outWorkspace = "C:/GISDATA/RoadData/output.gdb"

# Use ListFeatureClasses to generate a list of shapefiles in the
#  workspace shown above.
fcList = arcpy.ListFeatureClasses()

# Execute CopyFeatures for each input shapefile
for shapefile in fcList:
    # Determine the new output feature class path and name
    outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".shp"))
    arcpy.CopyFeatures_management(shapefile, outFeatureClass)
  • 1
    Welcome to GIS SE, your logic looks correct. Can you provide more details about what is not working (getting an error)? – artwork21 Jan 30 '18 at 13:25
  • 1
    I agree with @artwork21, your script looks good. What issues are you encountering. – Aaron Jan 30 '18 at 15:57
1

Your example is right from the Esri. It works but in your case you need to add arcpy.env.overwriteOutput = True after the import. This is because it worked for the first Shapefile as you were testing but the second time you ran it the featureclass existed and it wouldn't overwrite it. Thus causing it to fail.

  • Thanks for your contribution. I would not want overwrite set if there are duplicate fc names. Those instances should be handled rather than overwritten. – Aaron Jan 30 '18 at 15:54
  • Then try something like this arcpy function: if arcpy.Exists(shapefile): do nothing else: arcpy.CopyFeatures_management..... – Bill Chappell Jan 30 '18 at 21:22
0

List your shapefiles in in_features and let us know if it works.

# Name: FeatureClassToGeodatabase.py
# Description: Use FeatureClassToGeodatabase to copy feature classes
#  to geodatabase format

# Import modules
import arcpy

# Set environment settings
arcpy.env.workspace = 'C:/gisdata'

# Set local variables
in_features = ['climate.shp', 'majorrds.shp']
out_location = 'C:/GISDATA/RoadData/output.gdb'

# Execute FeatureClassToGeodatabase
arcpy.FeatureClassToGeodatabase_conversion(in_features, out_location)
  • Thank you for the overwrite suggestion. To clarify a bit, I am trying to take multiple shapefiles from a single folder without having to name each folder and output. Some folders have as many as 30 shapefiles. I am trying to organize these shapefiles into a geodatabase in each folder. I do not want to have to go through each folder, which there are at least 100 folders, identify each and every shapefile and modify the script to do so. I hope this clears things up a bit and I did not make it worse. – Gary Holmstrom Jan 31 '18 at 14:36
  • @GaryHolmstrom Please edit your original question to include these pertinent details. – Aaron Jan 31 '18 at 17:50
  • Easy way is to get a list of folders, and for each folder, copy the shapefiles in to the FGDB and append folder name to it (front or back of it). End result on file GDB with a bunch of Featureclasses. Other option is read folder name, make a dataset in the FGDB, copy shapefiles from folder into Dataset, step to next folder. ***However,all shapefiles in folder will need to be in same projection to be copied into dataset. – Bill Chappell Jan 31 '18 at 18:29

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.