Python Script Help - Spaces in filename

6924
7
Jump to solution
05-23-2012 04:13 AM
JamesMitchell
New Contributor II
Hello,

Please can somebody provide some advice?

I am using the code below in a script to add a field to multiple shapefiles, however when the filename or file location contains spaces it does not work. I get the error  message "Input Table: xxxxx does not exist or is not supported"

I think it must have something to do with the LayersList = string.split(arcpy.GetParameterAsText(0),";") line, but as i am new to Python i do not know how to correct it.

It will be greatly appreciated if someone can help me.

Thank you.

James

import arcpy, os, string

#Read input parameters from script tool
LayersList = string.split(arcpy.GetParameterAsText(0),";")

# Process: Add Field
for Layer in LayersList:
arcpy.AddField_management(Layer, "DUP", "DOUBLE", 9, "", "", "Dup", "NULLABLE")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
It's because arcpy is wrapping single quotes around the file path.
print repr(Layer) >>> u"'C:\\WorkSpace\\test workspace\\test.shp'"


This works:
import arcpy, os  #Read input parameters from script tool LayersList = arcpy.GetParameterAsText(0).split(";")  # Process: Add Field for Layer in LayersList:     arcpy.AddMessage(repr(Layer))     arcpy.AddField_management(Layer.strip("'"), "DUP", "DOUBLE", 9, "", "", "Dup", "NULLABLE")

View solution in original post

0 Kudos
7 Replies
JamesMitchell
New Contributor II
Thank you ver much for the quick response.

I have tried this and it still has the same error.

The input parameters are shapefiles.

Thanks
0 Kudos
MathewCoyle
Frequent Contributor
You should remove any spaces from your shapefile names.
0 Kudos
JamesMitchell
New Contributor II
Thank you,

The shapefiles are in different locations. When i save them into a folders without any spaces it works but when i move them into a folder location with spaces it no longer works.

Thanks for your input.
0 Kudos
MathewCoyle
Frequent Contributor
Spaces in path names are not supported either for some functions.
0 Kudos
Luke_Pinner
MVP Regular Contributor
It's because arcpy is wrapping single quotes around the file path.
print repr(Layer) >>> u"'C:\\WorkSpace\\test workspace\\test.shp'"


This works:
import arcpy, os  #Read input parameters from script tool LayersList = arcpy.GetParameterAsText(0).split(";")  # Process: Add Field for Layer in LayersList:     arcpy.AddMessage(repr(Layer))     arcpy.AddField_management(Layer.strip("'"), "DUP", "DOUBLE", 9, "", "", "Dup", "NULLABLE")
0 Kudos
JamesMitchell
New Contributor II
Hello,

Yes, that has corrected the issue. Thank you very much for the help.

James
0 Kudos
MaryM
by
Occasional Contributor
Thank you, this helped me figure out how to remove the extra quotes from my layer files that had spaces.

http://forums.arcgis.com/threads/94529-Layer-Invalid-Data-Source
0 Kudos