I have a Python script-based tool in ArcGIS. One of the parameters for the tool is an input raster file, which I've set (using the properties of the tool within the toolbox) to be a Raster Layer. This means that when I execute the tool I get the option to select that parameter from a dropdown list of currently loaded raster layers, or by navigating to find a new raster layer within the filesystem.

I am then using the standard arcpy.GetParameterAsText(0) code to get the parameter into my Python script. This works fine when I select a file by browsing through the filesystem, as the text in the dialog is the full path to the file, but when I just select from the dropdown list the text I get back is just the filename (eg. file.tif).

The code I am running needs to know the full path to the file - how do I get this?

If I could assume that the file was always in the workspace then I could append the filename to arcpy.env.workspace, but I can't assume that. Do I need to iterate through all of the layers that are loaded until I find one with the same name, and then find its full path, or is there an easier way?

  • 3
    I'm thinking that what you are choosing from the pulldown list is a layer name from your TOC (which happens to be the same as its filename). As an idea perhaps you can iterate through ListLayers to find that layer name and once found access its dataSource property to get the workspacePath and the datasetName properties combined. – PolyGeo Aug 23 '12 at 11:09
  • Why do you need the full path? This important part of the question might help someone answer your question with a work around. – Michael Markieta Aug 23 '12 at 14:43
  • @MichaelMarkieta: I'm running some code in my Python script that is using a completely separate library (outside of anything that arcpy provides) and that requires the full path of the input file for it to do its processing. – robintw Aug 23 '12 at 14:47

I had the same issue a while back. It's a pretty easy fix, just use the the describe tool.

Your already getting the layer name from your parameters. So all you have to do is describe the layer, find the path then merge the two.

layer = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(layer)
path = desc.path
layersource = str(path) + "/" + layer

That should do it no problem.

Hope this helps

| improve this answer | |
  • 1
    Just keep in mind if the Layer name in the TOC is not the same as the Feature Class, you will need to use desc.name in the above example. – Sethdd Feb 25 '15 at 1:08
  • What about case you do not know if layer is or is not full path? My solution is layersource = os.path.join(arcpy.Describe(layer).path, os.path.basename(layer)) - also not sure about using slash to join parts of path, I find os.path.join more safe... Need to add import os at the top. – Miro Sep 8 '17 at 13:45
  • You sir are a lifesaver. I've been beating my head up against my computer for the last several hours trying to figure out how to get my input paths to work with variables. The original code I was working with worked on the one project I ran it on, but then I got the 000732 "does not exist or is not supported" on the second project I ran it on. I tested the code manually (with full paths) in the Python window & it worked. This solution allowed me to pass the full path to the arcpy.TableToTable_conversion() tool with my variables & correct formatting. This solution was hard to find! Thanks! – Zachary Ordo - GISP Sep 18 '17 at 21:51

If you want to account for the possibility that the user might specify a raster within the filesystem:

from os.path import split, join
layer = arcpy.GetParameterAsText(0)

#Check if there is a path on the input parameter. If not, prepend the path.
if not split(layer)[0]:
    layer = join(arcpy.Describe(layer).path, "{}.tif".format(layer))
| improve this answer | |

There is a little bit shorter way to do this also. describe data objects have a catalogPath property which is the full path to the file.

With the 10.1 version we're using, you can do:

layer = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(layer)
layersource = desc.catalogPath
| improve this answer | |

You could use arcpy.GetParameter(0) rather than arcpy.GetParameterAsText(0) as this will get the layer object rather than just a string with the layer name.

If you can get a layer object you can get the Layer properties directly and saves having to do a Describe.

Something like this might get you what you want:

import arcpy, os

lyr = arcpy.GetParameter(0)

# Check this is a Layer Object
if hasattr(lyr, "dataSource"): 
    arcpy.AddMessage("Datasource = {}".format(lyr.dataSource))
    filepath = lyr.dataSource
    arcpy.AddMessage("Datasource = {}".format(str(lyr)))
    filepath = str(lyr)
| improve this answer | |

Here's what I ended up using. It works like a charm.

lyr = arcpy.mapping.ListLayers(mxd)[0]
desc = arcpy.Describe(lyr)
layersource = desc.catalogPath
print layersource
| improve this answer | |

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.