Select to view content in your preferred language

SaveToLayerFile_management() gives 732 err if input layername contains forward slash

1518
4
07-01-2013 07:46 AM
RobinPearce
Emerging Contributor

#  Change Feature Classes to Relative Path Lyr files

import arcpy, csv, os
from arcpy import env

env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
messageStr = str(mxd)
arcpy.AddMessage(messageStr)
arcpy.env.workspace = "C:\\GeneralWorkData\\ArcGIS Test Data\\Test Data From,to Martin B\\Relative_Paths_Lyr_Files"
for fc in arcpy.mapping.ListLayers(mxd):
  fc = str(fc)
  out_layer = fc + ".lyr"
  arcpy.SaveToLayerFile_management(fc,out_layer,"RELATIVE")
  messageStr = out_layer
  arcpy.AddMessage(messageStr)



This loops round all the layer names in the currently loaded data. Works fine until it hits layer called "Observed pipeline/cable", and gives the following message:

ERROR 000732: Input Layer: Dataset Observed pipeline/cable does not exist or is not supported". Failed to execute (SaveToLayerFile)."

Is it thinking the forward slash is part of a directory(which it isn't)? What do I do to make SaveToLayerFile() accept the forward slash as part of the layer name?
Tags (2)
0 Kudos
4 Replies
TonyAlmeida
MVP Regular Contributor
Forward slashes are not allowed in python.

use "os.path"

os.path.dirname(mxd.filePath)
0 Kudos
RhettZufelt
MVP Notable Contributor
The forward slash is not supported in part of the filename for saveaslayerfile tool. In fact, I can't even name a dataset with a slash in it. That must have been added in the TOC of the MXD itself?

In any case, it appears as if you are getting the list of layers from the current mxd, so suspect you have no control over the name in the TOC (would be easiest just to not have the slash in the name), so.

Could do something like this that would replace any forward slashes with an underscore (this is what ArcMap does if you right-click on that same layer in the MXD and select save as layer file).

#  Change Feature Classes to Relative Path Lyr files

import arcpy, csv, os
from arcpy import env

env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
messageStr = str(mxd)
arcpy.AddMessage(messageStr)
arcpy.env.workspace = "C:\\GeneralWorkData\\ArcGIS Test Data\\Test Data From,to Martin B\\Relative_Paths_Lyr_Files"
for fc in arcpy.mapping.ListLayers(mxd):
  fc = str(fc)
  out_layer = fc.replace("\\","_") + ".lyr"
  arcpy.SaveToLayerFile_management(fc,out_layer,"RELATIVE")
  messageStr = out_layer
  arcpy.AddMessage(messageStr)


R_

If this doesn't work, then perhaps you are running into this issue (from the help docs) that will also throw the 000732 error:

An invalid subtype on the dataset. To fix this, go to the feature class properties, then click the Subtypes tab and reenter the default subtype code. If the default is 0, click the cell with 0 and reenter that same value. Then apply the change by clicking OK. You will now be able to use the dataset.  
0 Kudos
RobinPearce
Emerging Contributor
The forward slash is not supported in part of the filename for saveaslayerfile tool. In fact, I can't even name a dataset with a slash in it. That must have been added in the TOC of the MXD itself? 

In any case, it appears as if you are getting the list of layers from the current mxd, so suspect you have no control over the name in the TOC (would be easiest just to not have the slash in the name), so. 

Could do something like this that would replace any forward slashes with an underscore (this is what ArcMap does if you right-click on that same layer in the MXD and select save as layer file). 

#  Change Feature Classes to Relative Path Lyr files

import arcpy, csv, os
from arcpy import env

env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
messageStr = str(mxd)
arcpy.AddMessage(messageStr)
arcpy.env.workspace = "C:\\GeneralWorkData\\ArcGIS Test Data\\Test Data From,to Martin B\\Relative_Paths_Lyr_Files"
for fc in arcpy.mapping.ListLayers(mxd):
  fc = str(fc)
  out_layer = fc.replace("\\","_") + ".lyr"
  arcpy.SaveToLayerFile_management(fc,out_layer,"RELATIVE")
  messageStr = out_layer
  arcpy.AddMessage(messageStr)


R_ 

If this doesn't work, then perhaps you are running into this issue (from the help docs) that will also throw the 000732 error:


Many thanks for the response, you are correct, we have been supplied with these layer names via a client's template. So we would rather not change the names but perhaps we will have to.

I'm afraid I don't understand your 'replace' function - surely that is for a backslash??

I tried manually saving the layer file by right clicking on the name as you mentioned but I get error message saying the layer(with the forward slash in it) cannot be found.

Do I have to rename the layer in the mxd before I even attempt this - if so, can this be done in Python script?

Rob P
0 Kudos
RhettZufelt
MVP Notable Contributor
Yes, this is for the backslash:

one would think that it should look something like this to replace all \ with _:

fc.replace("\","_") 


however, the backslash is a special character in python, so you need to "escape" it first by adding the other backslash.  (if you try it with one, it won't recongize the quotes and will be invalid)

So, if there is a backslas in the fc name, it will replace it with an underscore before save.

Same in ArcMap, you will get that error unless you remove the backslash from the filename.  When I right-click in arcmap and choose save as layer file, if my layer name is "Observed pipeline/cable", the name ArcMap suggests is "Observed pipeline_cable.lyr" and it works.  If I put the \ back in there, layer can't be found error (not very descriptive or accurate error I know).

Should not have to rename anything in the mxd first, since all we are saving is a layer file.  We just have to make sure the layer file name is valid.

R_
0 Kudos