I'm looking for the arcgis python equivalent of the VBA How to save a layer file with a relative path.
I'm using a script to change the paths in a collection of about 75 layer files before distributing to outside offices. The script works well, except that the new layer file paths are not relative, which means they usually are broken for the recipients as they rarely use the same drive letters etc. as we do, which pretty much defeats the purpose.
Paths follow the pattern X:\Layers\Category1\Group3
, and X:\data\One.gdb
, i.e. the layers and data are one the same drive but not in the same tree.
Best Answer
I found a solution: a) don't validate
replaceDataSource
(last parameter is False instead of True), and b) useSaveToLayerFile
which has a "relative" parameter instead ofsaveACopy
:Unfortunately it doesn't work with group layer files, but at least it's progress!
For your info and to avoid confusion, as the apparent contradiction certainly confused me at first, in the Esri doc Paths explained it states unequivocally that relative paths are not supported in scripts while the reference page for Save to Layer file says just as clearly there is a RELATIVE keyword.
What this means in practice is that yes you must use full path notation in scripts,
x:\data\foo.gdb
and not..\..\data\foo.gdb
, and then saveToLayer strips the drive letter and leading directories on save. So the inputs must always be full paths, while the output may or may not be.As far as I can tell SaveToLayerFile is the only arcpy command with a relative parameter. I presume there is an equivalent for saving mxd's but I haven't found it yet.