0

I'm using the Python window in ArcMap 10.3.1 to conduct Maximum Likelihood classifications (MLClassify, Spatial Analyst extension) on multiple images. I've done this with no issues before, but now when I run the code, the pathnames for some (but not all) of the inputs seem to be altered and cause errors.

Here is sample code I'm running in the Python window that defines my input files and runs the classify tool:

import arcpy  
from arcpy import env  
from arcpy.sa import *  

aug1951_clip_BW = "C:\Project\GIS\Grayscale\GrayAllYears.gdb\aug1951_clip_BW"                                #input raster  
aug1951_clip_bw_gsg = "C:\Project\GIS\Grayscale\aug1951_clip_bw.gsg"                                         #signature file, used in classifying  
Class_aug1951_clip_BW = "C:\Project\GIS\Grayscale\GrayAllYearsNewGSGOutput.gdb\Class_aug1951_clip_BW"        #name of output classified image  
ConClass_aug1951_clip_BW = "C:\Project\GIS\Grayscale\GrayAllYearsNewGSGOutput.gdb\ConClass_aug1951_clip_BW"  #name of optional confidence image output 

arcpy.gp.MLClassify_sa(aug1951_clip_BW, aug1951_clip_bw_gsg, Class_aug1951_clip_BW, "0.0", "EQUAL", "", ConClass_aug1951_clip_BW)

The MLClassify command then fails to execute--interestingly, in the error message that appears, the "\a" in the signature file (.gsg) pathname seems to be replaced with a black dot/bullet point. My guess was that this altered pathname is what renders the file in the argument unrecognizable and causes the following error:

ExecuteError: Failed to execute. Parameters are not valid. ERROR 000865: Input signature file: C:\Project\GIS\Grayscale•ug1951_clip_bw.gsg does not exist. Failed to execute (MLClassify).

However, this doesn't happen with my signature files that do not begin with the letter "A"!
Below, an example of a classification that ran with no errors, identical in all ways besides the input filenames:

sep1979_clip1_BW = "C:\Project\GIS\Grayscale\GrayAllYears.gdb\sep1979_clip1_BW"
sep1979_clip1_BW_gsg = "C:\Project\GIS\Grayscale\sep1979_clip1_BW.gsg"
Class_sep1979_clip1_BW = "C:\Project\GIS\Grayscale\GrayAllYearsNewGSGOutput.gdb\Class_sep1979_clip1_BW"
ConClass_sep1979_clip1_BW = "C:\Project\GIS\Grayscale\GrayAllYearsNewGSGOutput.gdb\ConClass_sep1979_clip1_BW"

arcpy.gp.MLClassify_sa(sep1979_clip1_BW, sep1979_clip1_BW_gsg, Class_sep1979_clip1_BW, "0.0", "EQUAL", "", ConClass_sep1979_clip1_BW)

Why is this bullet point inserted in the middle of my signature file pathname/how can I stop this from occurring, and how can I execute MLClassify for these images without having to rename my signature files that begin with "A"? This is the only trend I find in common among the images that have failed to run.

7

The problem is you need to be escaping the pathnames. In Python (and many other programming languages) the single backslash \ is an escape character. See this page for an explanation.

in the path "C:\Project\GIS\Grayscale\aug1951_clip_bw.gsg", the \a is an ascii Bell. You can see all the string literals here.

You can properly format your pathnames many different ways:

double backslash:

"C:\\Project\\GIS\\Grayscale\\aug1951_clip_bw.gsg"

forward slashes (how separators are in linux/mac):

"C:/Project/GIS/Grayscale/aug1951_clip_bw.gsg"

Or the easiest, just use the raw string syntax by prefixing with a lowercase r:

r"C:\Project\GIS\Grayscale\aug1951_clip_bw.gsg"

3

You are using a syntax for your paths that includes a backslash ("\"). This is a special character to Python and so whenever you specify paths you need to account for it.

Instead of:

aug1951_clip_bw_gsg = "C:\Project\GIS\Grayscale\aug1951_clip_bw.gsg"

which leads to that bullet point and error, try:

aug1951_clip_bw_gsg = r"C:\Project\GIS\Grayscale\aug1951_clip_bw.gsg"

or

aug1951_clip_bw_gsg = "C:\\Project\\GIS\\Grayscale\\aug1951_clip_bw.gsg"

or

aug1951_clip_bw_gsg = "C:/Project/GIS/Grayscale/aug1951_clip_bw.gsg"

(and do the same for your other paths, in every Python program that you write)

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.