I am very new to Python, and I'm trying to create a function where I'm copying over feature classes from a gdb to different folder locations as shapefiles. The script needs to be reading from a textfile that lists the different path locations. Here is a screenshot of the textfile:
So if the feature class from the gdb is "apd" it needs to copy over to "\DataSets\APD\apd.shp", if it's "comps", it needs to be copied over to "\DataSets\Completion\comps.shp", and so on. The function needs to have two parameters (the input and the output). The input being the feature class and the output being the destination path.
How do I define my destination parameter so that a particular feature class needs to be copied over to that specific folder?
Sadly, my code is looking something like this:
import arcpy
import os
arcpy.env.workspace = 'GOM3.gdb\\NAD1927'
featureclasses = arcpy.ListFeatureClasses()
def CopyFeature (fc, dest):
for fc in featureclasses:
if arcpy.Exists(fc):
fname="X:\GOM-Cubed\DataSets\ShapefileList.txt"
with open(fname) as f:
content = f.readlines()
for dest in content:
arcpy.CopyFeatures_management(fc, dest)
fname.close()
Eventually I want to add this script to ModelBuilder. That way the user can define the input parameter.
-
2Why re-read the file for each feature class? Read it once into a list and be done with it.Vince– Vince2018年11月29日 19:09:03 +00:00Commented Nov 29, 2018 at 19:09
1 Answer 1
I'm not sure this is the very best method, but here is what occurs to me. The difficult part of what you are doing is connecting the right feature class to the correct path and shapefile (and this just can't be automated from what I see in the paths you have outlined). Instead of a reading in a text file, you could just define a dictionary above the function that you've written. Vince also makes a good point (if you are going to read in a file, read it into a list), but you could do away with reading in the file altogether if you do it as I suggest (or read in a python file with just the dictionary in it). If you are making it flexible as you said for model builder it would force a user to construct a dictionary file as input but it could be done as a system argument.
The way I'm writing this code assumes all the feature classes in the gdb are in the same fc but since it looks like you are having to write up this text file with the custom paths and everything, you could modify it as you see fit.
This code is untested but as I go over it, I think it may be an improvement in the overall approach?
import arcpy
import os
arcpy.env.workspace = 'GOM3.gdb\\NAD1927'
featureclasses = arcpy.ListFeatureClasses()
file_list = {
"apd":r"C:\DataSets\APD\apd.shp",
"comps":r"C:\DataSets\Completions\comps.shp",
"aliquot":r"C:\DataSets\CurrentLease\aliquot.shp"
}
def CopyFeature (fc, dest):
for fc in featureclasses:
if arcpy.Exists(fc):
arcpy.CopyFeatures_management(fc, dest)
for key, value in file_list.iteritems():
fc = os.join(arcpy.env.workspace, key)
CopyFeature(fc, value)
Explore related questions
See similar questions with these tags.