I am trying to write a python script that would allow me to download data and copy it to a file geodatabase. The script currently downloads and extracts a zip folder from a FTP site. Ideally I would like my script to then copy the extracted shapefile into a geodatabase. The workspace in which the data is extracted remains constant. Where as the name of the data that is downloaded and extracted is variable. How can I reference the downloaded data regardless of its name for the input argument of the CopyFeature tool?
arcpy.env.workspace = r'C:\Users\Desktop\ExtractedData'
arcpy.CopyFeatures_management( downloaded shapefile, 'C:\Users\Desktop\Test.gdb')
2 Answers 2
If you know the folder use ListFeatureClasses to get a list of the feature classes in your workspace:
import os, arcpy
arcpy.env.workspace = r'C:\Users\Desktop\ExtractedData'
for ThisFC in arcpy.ListFeatureClasses():
GDBName,Ext = os.path.splitext(ThisFC) # splits out name and extension
GDBName = GDBName.replace(" ","_") # get rid of spaces in shapefile names
arcpy.CopyFeatures_management( ThisFC, r'C:\Users\Desktop\Test.gdb' + GDBName)
-
An ancillary goal for my script is to incorporate it into an arcgis toolbox. Would you have any suggestions as how I could set the output for the copy feature tool to a multivalue - GetParameterAsText? So I can copy the data to multiple geodatabases?Lotalota– Lotalota2015年09月14日 22:59:44 +00:00Commented Sep 14, 2015 at 22:59
-
I always use sys.argv[] to get the parameters, but an output is slightly different.. I think you need to Get the parameter as normal then use SetParameterAsText to enforce it at the end of the script, have a read of help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… about input/output parameters.Michael Stimson– Michael Stimson2015年09月14日 23:13:03 +00:00Commented Sep 14, 2015 at 23:13
I think you should look at the first ListFeatureClasses example in the online help for arcpy.ListFeatureClasses() for ideas on how to incorporate that function.
It has example code to:
Copy shapefiles to a geodatabase
i.e. very similar to what you are trying to do in the code snippet that you have presented.
-
1Beat me by one minute PolyGeo. At least the answers are consistent.Michael Stimson– Michael Stimson2015年09月14日 22:28:07 +00:00Commented Sep 14, 2015 at 22:28