0

我的代码:

#  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)

这将循环当前加载数据中的所有图层名称。工作正常,直到它到达名为“Observed pipeline/cable”的层,并给出以下消息:

错误 000732:输入层:观察到的数据集管道/电缆不存在或不受支持“。执行失败(SaveToLayerFile)。”

它是否认为正斜杠是目录的一部分(它不是)?我该怎么做才能使 SaveToLayerFile() 接受正斜杠作为图层名称的一部分?

4

1 回答 1

0

必须对那个元字符做点什么。可能最好用下划线替换,fc.replace('/','_') + '.lyr'. 可以尝试插入反斜杠来逃避对该字符的解释,但这可能会变得混乱。

于 2013-09-11T00:45:32.887 回答