2

I have a Geodatabase full of featureclasses. I want to perform the following:

  1. Copy out each individual featureclass as an output individual shapefile (and have the same name).
  2. Create individual Output Folders (of the same output shapefile name).
  3. Copy each output shapefile into its own unique folder (of the same name).

Currently, I can perform task 1 and 2 above, however, I am having trouble with task 3 above. My results so far is that I can create the output shapefiles and folders (of the same name), however, the shapefile outputs do not sit in the pertaining folders. i.e. My result = a bunch of shapefiles and a bunch of folders. I need to (i) either have the shapefiles created and automatically placed into the pertaining folder (based on the same name of the shapefile), or (ii) After creating the shapefiles and folders, move or copy the output shapefiles into each pertaining folder (based on the same name as the shapefile).

Therefore, for each of my featureclasses, my end result would be 1 seperate folder housing 1 shapefile (same name for both folder and shapefile).

I am fairly new to Python.

Input vs Current Output vs INTENDED OUTPUT

# import system modules
import arcpy
import os
from arcpy import env

# Set environment settings
# Note: The INPUT Geodatabase below houses 5 x featureclasses
env.workspace = r"D:\INPUT.mdb"   
arcpy.env.overwrite = True

# Set local vairables
# Note: Ensure that you have this empty folder already created within FileManager etc.
outWorkspace = "D:/OutputFoldersAndSHP"

print "Commencing"

# Use the ListFeatureClasses function to generate a list of featureclasses in the env.workspace further above
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    # PHASE 1: CREATE A FOLDER BASED ON INDIVIDUAL FC NAMES
    # Note: (It simply uses the fc name as the output folder name!) 
    arcpy.CreateFolder_management(outWorkspace, fc)

    # PHASE 2: COPY FEATURECLASSES TO SHAPEFILES
    # Determine the new output shp path and name
    outSHP = os.path.join(outWorkspace, fc)
    arcpy.CopyFeatures_management(fc, outSHP)

print "Completed"
2

You need to specify the output shapefile. So the last line should be

arcpy.CopyFeatures_management(fc, os.path.join(outSHP, fc + '.shp'))

That should create the directory structure you're looking for.

  • @Jim, I just tested this, and it works. You need to keep the create folder management section. – JamesLeversha Nov 22 '16 at 4:18
  • BINGO!!! FANTASTIC! THIS WORKED PERFECTLY... – James Nov 22 '16 at 4:19
1

Try this:

for fc in fcs:
    # PHASE 1: CREATE A FOLDER BASED ON INDIVIDUAL FC NAMES
    # Note: (It simply uses the fc name as the output folder name!) 
    outSHP = arcpy.CreateFolder_management(outWorkspace, fc)

    # PHASE 2: COPY FEATURECLASSES TO SHAPEFILES
    arcpy.CopyFeatures_management(fc, outSHP)

print "Completed"

I tested this and outSHP returns the path to the folder.

  • so does the outSHP = os.path.join(outWorkspace, fc) line though – JamesLeversha Nov 22 '16 at 4:03
  • Unfortunately, the 'Try this' response above still has folders and shp output (not shp within folder). Thank you so much for your quick reply and attempt. Your help was much appreciated though – thank you! – James Nov 22 '16 at 23:07

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.