I need the exact source path of layer files as shown by QGIS Layer Properties Information Window:

enter image description here

I know how to retrieve the layer source URI (and using the data provider gives the same result):

    layer = qgis.utils.iface.activeLayer()
    uri = layer.source()
    # uri = layer.dataProvider().dataSourceUri()

Now retrieving the exact layer source path from the URI is not a trivial thing, as it contains information depending on the data provider type.

layer.providerType() == 'delimitedtext' can look something like that:


layer.providerType() == 'ogr' could look like that:

'/vsizip//home/ubuntu/QGIS/Ciutat pròxima 1.0/DEMOGRAFIA/BCN_UNITATS_ADM.zip/0301100100_UNITATS_ADM_POLIGONS.json|layername=0301100100_UNITATS_ADM_POLIGONS|geometrytype=Polygon|subset="CONJ_DESCR"=\'Districtes\''

So it's actually possible to get the layer source path as shown in QGIS, but is there a easy way to do that?

PS: I know this question in a quite similar way has been asked before, but always in a different context or with ambiguous descriptions, like this one (which asked 2 questions at the same time) and this one (which asks about running it as a daemon).

  • I don't think one line method like source exists. But you can check provider type and split the given path by specific character like ?, | to get the source file path. Commented Dec 8, 2022 at 14:53
  • The problem is that after splitting the uri (better by os.path.split(uri) or os.path.basepath(uri)) you still don't have a valid path as shown by the 2 examples above. For sure it's possible to remove file:// and /vsizip/, but I need a more secure way of doing that, is has to work for all possible provider types.
    – geraldo
    Commented Dec 8, 2022 at 15:05

1 Answer 1


There is a method called decodeURI which can be used to break down the data source URI into the core path and all other components.

The documentation states that this function may not be supported by all provider types, but for delimited text layers and ogr it definitely works as shown below:

>>> layer = iface.activeLayer()
>>> uri_components = QgsProviderRegistry.instance().decodeUri(layer.dataProvider().name(), layer.publicSource());
>>> layer.source()

>>> uri_components['path']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.