Move all the shape files to Geodatabase using arcpy

11-11-2021 03:01 AM
Labels (1)
New Contributor

I want to move all my shape files to the geodatabase, with a single command, means I don’t want to manually copy one by one shape file into the geodatabase.

Below is the code I am using, but unable to copy my data from shape file to the geodatabase.

import arcpy
arcpy.env.workspace = "d:/roads_shp"
fcs = arcpy.ListFeatureClasses("*")

arcpy.env.workspace = "Database Connections/Bluestar.sde"

for fc in fcs: 
0 Kudos
6 Replies
MVP Emeritus

Are you getting any errors?  Are there any other files in d:/roads_shp?

Here is some python I whipped up that works for me.  There may be other approaches using walk() and or glob() ; I don't like to over complicate things:



import arcpy,os

shapeDir = r'C:\Temp\my_shapes'
arcpy.env.workspace = shapeDir
targetGDB = r'C:\Temp\TargetGeodatase.gdb'
for file in os.listdir(shapeDir):
    if file.endswith(".shp"):
for shapeFile in shapeFileList:
    newName = shapeFile.split('.')[0]


Pretty straight forward; get a list of shapefiles and step through it.  Use the 'root' name of the shapefile for your geodatabase feature class and let it rip.





That should just about do it....
by Anonymous User
Not applicable

Thanks for posting that! You just helped me cut my script down from about 90 lines haha.

0 Kudos
New Contributor

CopyFeatures_management requires two parameters - where are you copying it to? 

0 Kudos
Frequent Contributor III

Are you doing this once or on a regular basis?

If doing it once why not simply use the tool in ArcCatalog to move all the shape files at once.

Or in Catalog, select all the shape files and drag them to your data base

If on a regular basis set it up using Model builder and then export the model to a python script.

New Contributor

Moving new one to the gdb is easy. Original ones I think is pretty straight forward. I can write some code later if you want. Files in TOC are not in the 'air' you can check their source in properties. With that info we can work out something out. 

0 Kudos
Occasional Contributor III

I am not an expert in Python but perhaps you can help to understand what is that you are exactly trying to achieve here. 

Are you trying to bring multiple shapefiles into a single feature class or you want one feature class for each shapefile?

If you wish to do perform the latter then you are better off using the Feature Class to Geodatabase tool. This tool is basically designed to work on batches of data that need to be migrated into a geodatabase. 

If you open the link you should be able to see the Python snippet for the same.



0 Kudos