1

I´m following this link Merge shapefiles in folders and subfolders with arcpy very very close because it really helps me to solve my problem. However, I rebuilt the script to accomplish my job but when I run the script to merge some shapefile, I get the next error:

this my rebuilt script:

import arcpy, os, sys, string, fnmatch
import arcpy.mapping
from arcpy import env

rootpath = r'F:\My_Shps\My_zone'
shp = 'mojonera50_p.shp'
counter = 0
matches = []
for root, dirs, files in os.walk(rootpath):
    for shps in files:
        if shps == 'mojonera50_p.shp':
           match =(os.path.join(root, shps))
           matches.append (match)
           counter = counter +1

arcpy.Merge_management(matches,r'F:\My_Shps\My_zone\Result')
print "Se unio la capa {0}".format(shp)

arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000735: Input Datasets: 
Value is required Failed to execute (Merge).

I think when I running the script it doesn't find the shapefile, but how Can I modify the script to get a simple message warning that shapefile is not in the rootpath

1

Try using os.path.isfile(shapefile)

You can do something like:

for shp in files:
    if os.path.isfile(shapefile):
        print("Shape file exists")
        # Do processing
    else:
        print("Shapefile does not exist")

Alternatively, you could raise an exception if the shapefile does not exist with the following line:

raise Exception("Shapefile does not exists")

which would stop the execution of the script.

  • Thanks for answering the question, but nothing occurs, I get the same error!! What else can I do? – Victor Quiroz Barrientos Jan 29 '18 at 20:50
3

Two things - you are already set up to check if you are not finding any input shapefiles with your counter variable. Simply add the following line to test if your script isn't finding anything:

rootpath = r'F:\My_Shps\My_zone'
shp = 'mojonera50_p.shp'
counter = 0
matches = []
for root, dirs, files in os.walk(rootpath):
    for shps in files:
        if shps == 'mojonera50_p.shp':
           match =(os.path.join(root, shps))
           matches.append (match)
           counter = counter +1

if not counter == 0:  #This line will catch if there are no inputs
    arcpy.Merge_management(matches,r'F:\My_Shps\My_zone\Result.shp')
    print "Se unio la capa {0}".format(shp)
else:
    print "Error, ningun input"

More importantly, I think the error is resulting from the fact that your output in the arcpy call (named RESULT) is currently named like a feature class, which can only be an output in a geodatabase. Add the ".shp" like I show above and it should function as intended.

  • Thank you For your help. That's what I need it. I didn't know how to use the counter – Victor Quiroz Barrientos Jan 29 '18 at 22:42
  • No problem - the other thing you can do is remove the counter variable entirely, and check to see if the matches list is empty, with the line "if len(matches) > 0:" – AlecZ Jan 30 '18 at 17:31
1

arcpy has a function to check for the existence of files called Exists(). This is particularly handy for checking for data within geodatabases. You would use the function as follows:

if arcpy.Exists(shps): 
    print "do something..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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