1

I have a question regarding the merge of a lot of folders, which has a lot subfolders, which has the shapefiles stored. I would like to merge all those in one geodatabase. So I thought to use a Python script.

import os, arcpy.da

print os.getcwd()

for dirname, dirnames, filenames in os.walk('.'):
    for subdirname in dirnames:
        current_dir =  os.path.join(dirname, subdirname)
        arcpy.env.workspace = current_dir
        fcList = arcpy.ListFeatureClasses(".shp")
        Merge = r"D:\Test\Merge_multiple.gdb\Totals"
        for fc in fcList:
            print fc
            arcpy.Merge_management([fc,destination],Merge)
            arcpy.Delete_management(desination)
            arcpy.Rename_management(Merge, destination)
        break

So, I started the script and after a minute it stops and unfortunately, no result. Because there are several different names in the shapefiles, I thought to use only the suffix .shp.

Could you help me?

[Edit] I get the path of the working directory in the python window. After this message, the window closes without any errors. enter image description here

[Edit2] Based on PolyGeo's advice, I've run the script also in the IDLE environment and this is the result:

enter image description here

3
  • Would you be able to edit your question to include what is printed back to your Python window when you run this script, please? And also include any error messages that you may be receiving.
    – PolyGeo
    Aug 21, 2014 at 11:10
  • Have you tried running it from an IDE like IDLE which comes with Python? That message looks like you have perhaps double-clicked on a *.py to execute it.
    – PolyGeo
    Aug 21, 2014 at 11:59
  • Yes, see the second Edit for the results.
    – McMave
    Aug 21, 2014 at 12:38

1 Answer 1

2

To copy shapefiles from multiple folders into a single geodatabase, you could do this:

import arcpy
import os
ws = #path to input folder
dst = #path to output geodatabase
for dirpath, dirnames, filenames in arcpy.da.Walk(ws,datatype="FeatureClass"):
    for file in filenames:
      print file
      filepath = os.path.join(dirpath,file)
      outpath = os.path.join(dst,file[:-4]) #drops the .shp extension
      arcpy.CopyFeatures_management(filepath,outpath)
2
  • 1
    Using os.path.splitext(file)[0] or file.split('.')[0] instead of file[:-4] would allow this method to copy feature classes from geodatabases, while keeping their full name intact.
    – nmpeterson
    Aug 21, 2014 at 14:18
  • Well, that dit it! It's running now and the results are great. Thanks!
    – McMave
    Aug 25, 2014 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.