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
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)
2 Answers 2
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– user301842015年08月20日 20:22:32 +00:00Commented Aug 20, 2015 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– Farid Cheraghi2015年08月20日 20:28:42 +00:00Commented Aug 20, 2015 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– user301842015年08月20日 20:39:46 +00:00Commented Aug 20, 2015 at 20:39
-
2@user30184 it says right in the description you posted that dashes
-
are valid for shapefiles.ianbroad– ianbroad2015年08月20日 20:44:57 +00:00Commented Aug 20, 2015 at 20:44 -
Yes, you are absolutely right, I was blind while reading.user30184– user301842015年08月20日 21:13:14 +00:00Commented Aug 20, 2015 at 21:13
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– Adam2015年09月13日 23:14:13 +00:00Commented Sep 13, 2015 at 23:14
-
1Sure 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– Emil Brundage2015年09月13日 23:17:26 +00:00Commented Sep 13, 2015 at 23:17
-
1Underscores ("_") are allowed but hyphens ("-") are not.Adam– Adam2015年09月13日 23:22:34 +00:00Commented Sep 13, 2015 at 23:22
-
They also aren't alphanumeric, meaning if the occur in the file name they'll be renamed.Emil Brundage– Emil Brundage2015年09月13日 23:23:52 +00:00Commented Sep 13, 2015 at 23:23
-
1Underscores (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– Adam2015年09月13日 23:24:57 +00:00Commented Sep 13, 2015 at 23:24
Explore related questions
See similar questions with these tags.
sample.shp
and it worked fine so do you still need to see other process ?!