I have a Geodatabase full of featureclasses. I want to perform the following:
- Copy out each individual featureclass as an output individual shapefile (and have the same name).
- Create individual Output Folders (of the same output shapefile name).
- 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"
-
Try using desktop.arcgis.com/en/arcmap/10.3/tools/conversion-toolbox/…. What version of arcgis are you using? Please add the tag to your questionJamesLeversha– JamesLeversha2016年11月22日 03:54:10 +00:00Commented Nov 22, 2016 at 3:54
2 Answers 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– JamesLeversha2016年11月22日 04:18:49 +00:00Commented Nov 22, 2016 at 4:18
-
BINGO!!! FANTASTIC! THIS WORKED PERFECTLY...James– James2016年11月22日 04:19:36 +00:00Commented Nov 22, 2016 at 4:19
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 thoughJamesLeversha– JamesLeversha2016年11月22日 04:03:34 +00:00Commented Nov 22, 2016 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– James2016年11月22日 23:07:40 +00:00Commented Nov 22, 2016 at 23:07