I'm trying to save all layers in my mxd as .lyr files using the script below. However, I'm getting an error because some layer names contain special characters (e.g. © : * ) and cannot be written into file names. How can I make sure special characters are ignored/replaced?
- import os
- basepath = r'C:\Users\Anneka\CaBA_Data_Phase_5\Layers'
- mxd = arcpy.mapping.MapDocument("CURRENT")
- for lyr in arcpy.mapping.ListLayers(mxd):
- if lyr.isGroupLayer == True:
- grpPath = os.path.join(basepath, str(lyr))
- if not os.path.exists(grpPath):
- os.makedirs(grpPath)
- else:
- fn = os.path.join(basepath, str(lyr) + ".lyr")
- lyr.saveACopy(fn)
- print "Saved: " + fn