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

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 29, 2018 at 20:30

3 Answers 3

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.

answered Jan 29, 2018 at 22:20
2
  • Thank you For your help. That's what I need it. I didn't know how to use the counter Commented Jan 29, 2018 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:" Commented Jan 30, 2018 at 17:31
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.

ahmadhanb
41.8k5 gold badges55 silver badges109 bronze badges
answered Jan 29, 2018 at 20:39
1
  • Thanks for answering the question, but nothing occurs, I get the same error!! What else can I do? Commented Jan 29, 2018 at 20:50
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..."
answered Jan 29, 2018 at 22:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.