5

I have shapefile named `band-tailed_pigeon.shp which is fine and no problem on displaying or geoprocessing on Desktop but in ArcPy when I try to merge some layes to it I am getting this error

enter image description here

ExecuteError: ERROR 000354: The name contains invalid characters

Failed to execute (Merge).

I tried to do the proccess with a simple name like sample.shp which it works but I HAVE TO keep the same name as we are using them in several projects.

Here is the code which I have

FinaloutLocation = "E:\\GIS\\Data\\Final\\band-tailed_pigeon.shp"
inFeaturesToMerge = [dissolve_1, dissolve_2, dissolve_3, dissolve_4, dissolve_5]
arcpy.Merge_management(inFeaturesToMerge, FinaloutLocation)
  • what are dissolve_1, dissolve_2, ...? please share your complete code sample. From your piece of code nobody can help you! – Farid Cheraghi Aug 20 '15 at 18:50
  • @FaridCher, as I mentioned the issue is in naming part and I test this with a name like sample.shp and it worked fine so do you still need to see other process ?! – Behseini Aug 20 '15 at 18:56
  • 5
    Its not generally a good idea to have special characters (like hyphens) in your file names for this very reason. I would suggest talking to whomever insists on this name and explain why its not a good idea. You could temporarily change the name to something without the dash and then rename the file after processing. – landocalrissian Aug 20 '15 at 19:02
  • Please always include the text, rather than just a picture, of any error message that you receive so that it becomes available for future searches by those in need. – PolyGeo Aug 21 '15 at 8:21
  • I agree with Polygeo. please edit the question and add the error as clear text – Farid Cheraghi Aug 21 '15 at 13:38
10

The problem is with the dash (-) in the shapefile name. This restriction is from geoprocessing tool and not the shapefile name since you can have dashes in your shapefile name.

Some Geoprocessing methods allow you to validate table (feature class) and field names. Consider using either the ValidateTableName or ValidateFieldName function to ensure your names are valid

  • The specification is in esri.com/library/whitepapers/pdfs/shapefile.pdf and it actually says: All file names adhere to the 8.3 naming convention. The main file, the index file, and the dBASE file have the same prefix. The prefix must start with an alphanumeric character (a–Z, 0–9), followed by zero or up to seven characters (a–Z, 0–9, _, -). The suffix for the main file is .shp. The suffix for the index file is .shx. The suffix for the dBASE table is .dbf. All letters in a file name are in lower case on operating systems with case sensitive file names. – user30184 Aug 20 '15 at 20:22
  • As I mentioned , this restriction comes from geoprocessing functions not shape file name. To work with Merge tool you have to remove dashes. – Farid Cheraghi Aug 20 '15 at 20:28
  • Yes, but still all shapefiles having dashes in names are invalid according to the specification. But so would be names which are longer than 8+3 characters so it is no wonder that the spec is not honored. I believe ESRI themselves are not honoring it. – user30184 Aug 20 '15 at 20:39
  • 2
    @user30184 it says right in the description you posted that dashes - are valid for shapefiles. – ianbroad Aug 20 '15 at 20:44
  • Yes, you are absolutely right, I was blind while reading. – user30184 Aug 20 '15 at 21:13
5

If you're having issues with shapefile names, a quick script before your merge to find the culprits and make a new copy of the shapefiles will fix the problem. Iterate through each shapefile, test its name, and copy if needed. Merge, then delete whatever new shapefiles you created.

Something like this (untested):

import arcpy
import os

inFeaturesToMerge = [dissolve_1, dissolve_2, dissolve_3, dissolve_4, dissolve_5]

#empty list for merge
mergeFcs = []

#empty list for delete
garbage = []

#iterate feature classes
for fc in inFeaturesToMerge:

    #directory of shapefile
    shpPath = os.path.dirname (fc)

    #set workspace to shapefile directory
    arcpy.env.workspace = shpPath

    #Get shapefile name
    shpName = os.path.basename (fc)

    #Check shapefile name to see if it's kosher
    if not shpName.lower().replace ("_", "").rstrip (".shp").isalnum():

        #Create unique name
        uName = arcpy.CreateUniqueName ("file.shp")

        #create new shapefile with new legit name
        arcpy.CopyFeatures_management (fc, uName)

        #add to list to be merged
        mergeFcs += [uName]

        #add to list to be deleted
        garbage += [uName]
    else:
        #add legit name to merge feature classes
        mergeFcs += [fc]


#merge
arcpy.Merge_management(mergeFcs, FinaloutLocation)

#Clean up
for trash in garbage:
    arcpy.Delete_mangement (trash)

A faster variation on this script is to use Rename_management, rename the culprits, and then change the name back after the merge. This one's a bit dangerous though, as if it fails halfway though you'll have lost some file names:

import arcpy
import os

inFeaturesToMerge = [dissolve_1, dissolve_2, dissolve_3, dissolve_4, dissolve_5]

#empty list for merge
mergeFcs = []

#empty dictionary to track renames
nameChangeDi = {}

#iterate feature classes
for fc in inFeaturesToMerge:

    #directory of shapefile
    shpPath = os.path.dirname (fc)

    #set workspace to shapefile directory
    arcpy.env.workspace = shpPath

    #Get shapefile name
    shpName = os.path.basename (fc)

    #Check shapefile name to see if it's kosher
    if not shpName.lower().replace ("_", "").rstrip (".shp").isalnum():

        #Create unique name
        uName = arcpy.CreateUniqueName ("file.shp")

        #create new shapefile with new legit name
        arcpy.Rename_management (fc, uName)

        #Add changed name to dictionary
        nameChangeDi [uName] = fc

        #add to list to be merged
        mergeFcs += [uName]

    else:
        #add legit name to merge feature classes
        mergeFcs += [fc]


#merge
arcpy.Merge_management(mergeFcs, FinaloutLocation)

#Rename back changed shapefiles
for newName in nameChangeDi:
    arcpy.Rename_management (newName, nameChangeDi [newName])
  • your first solution doesn't look like it accounts for the actual problem with the filename the OP has a problem with - the hyphen. – Adam Sep 13 '15 at 23:14
  • Sure it does. It checks if the file name, sans underscores, is all alphanumeric, and copies the feature class to a new one with a valid name if it doesn't. – Emil Brundage Sep 13 '15 at 23:17
  • Underscores ("_") are allowed but hyphens ("-") are not. – Adam Sep 13 '15 at 23:22
  • They also aren't alphanumeric, meaning if the occur in the file name they'll be renamed. – Emil Brundage Sep 13 '15 at 23:23
  • Underscores (eg: 'my_shapefile_name.shp') are allowed. It is hyphens (eg: 'my-shapefile-name.shp') which are disallowed. you need to swap the underscore in the if statement for a hyphen. Also, there are other non-alphanumeric characters which are allowed (such as a hash/pound '#'). – Adam Sep 13 '15 at 23:24

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.