2

I am trying to run a python script to merge some files in a folder test, called test1.shp, test2.shp, test3.shp. The code is this:

import os
import arcpy    
workspace = "C:/mypath/test/"       
prefixed = [filename for filename in os.listdir(workspace) if filename.startswith("test")]   
total = "C:/mypath/test/Merged.shp"    
arcpy.Merge_management(prefixed, total)

and I keep having the same error:

ERROR 000732:  Dataset test1.cpg;test1.dbf;test1.prj;test1.sbn;test1.sbx;test1.shp;test1.shp.xml;test1.shx;test2.cpg;test2.dbf;test2.prj;test2.sbn;test2.sbx;test2.shp;test2.shp.xml;test2.shx;test3.cpg;test3.dbf;test3.prj;test3.sbn;test3.sbx;test3.shp;test3.shp.xml;test3.shx does not exist or is not supported Failed to execute (Merge).

I tried to force to merge only .shp files adding 'and filename.endswith(".shp"), and I have the same error, only with only .shp files.

I tried with a list in which each file has the entire path:

l =[]
for i in prefixed:
     l.append ('"'+workspace +i+'"')    
total = "C:/mypath/test/Merged.shp"    
arcpy.Merge_management(l, total)

I tried to write my path using ' or using '//' instead, but I always have the same error message.

If I try a simple:

total = "C:/mypath/test/Merged.shp"    
arcpy.Merge_management(["C:/mypath/test/test1.shp", "C:/mypath/test/test2.shp", "C:/mypath/test/test3.shp"], total)

everything works fine.

Can anybody help me?

1 Answer 1

3

Use ListFeatureClasses with a wildcard:

Lists the feature classes in the workspace, limited by name, feature type, and optional feature dataset

You dont need to worry about other files since only feature classes will be listed.

import arcpy

folder = r'C:\shapefile_folder'
arcpy.env.workspace = folder
total = r'C:\mypath\test\Merged.shp'

shapefiles = arcpy.ListFeatureClasses(wild_card='*test*')
arcpy.Merge_management(inputs=shapefiles, output=total)
0

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.