Merge 在python中遍历文件夹并将shapefile提取到地理数据库

Merge 在python中遍历文件夹并将shapefile提取到地理数据库,merge,arcpy,Merge,Arcpy,嗨,我正在使用样本数据。在一个名为Shapefile的文件夹中,我有3个文件夹;每个文件都有3个形状文件,分别命名为Hazard1.shp、Hazard2.shp、Hazard3.shp(每个文件中大约有3000条记录)。我试图遍历这些文件夹,从每个文件夹中提取Hazard1.shp,并将其合并到名为totals的地理数据库中的要素类中。此代码适用于数百个文件夹,但我使用了3个示例。我运行我的代码,没有错误,但当我检查总数时,只有3000条记录-从合并中应该有12000条。我做错了什么 impo

嗨,我正在使用样本数据。在一个名为Shapefile的文件夹中,我有3个文件夹;每个文件都有3个形状文件,分别命名为Hazard1.shp、Hazard2.shp、Hazard3.shp(每个文件中大约有3000条记录)。我试图遍历这些文件夹,从每个文件夹中提取Hazard1.shp,并将其合并到名为totals的地理数据库中的要素类中。此代码适用于数百个文件夹,但我使用了3个示例。我运行我的代码,没有错误,但当我检查总数时,只有3000条记录-从合并中应该有12000条。我做错了什么

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("Hazard1.shp")
        destination = r"F:\Extraction\GeoDatabase\Total.gdb\totals"
        for fc in fcList:
            print fc
            arcpy.Merge_management(fc,destination)
        break

每次迭代文件夹时,都会覆盖“总计”要素类。你应该:

  • 在循环外部创建一个空列表,并在每次迭代时向其追加fc,然后合并此列表中的所有项
  • 或者,更简单地说,使用
    Append
    而不是
    Merge
    (如果“totals”已经存在)
添加:

Merge = r"F:\Extraction\GeoDatabase\Total.gdb\totals_Merge"
那么您的for循环应该是:

    for fc in fcList:
        print fc
        arcpy.Merge_management([fc,destination],Merge)
        arcpy.Delete_management(destination)
        arcpy.Rename_management(Merge, destination)