1

I want to get the Geopackage file path where my layer is stored in.

Layer = QgsProject.instance().mapLayersByName('DAT')[0]
print(Layer.source())

But this only gives me the path with layer pointer:

/home/.../DAT.gpkg|layername=DAT

My approach was to do:

Layer = Layer.source().split("|")[0]

It works, but is there a better way?

2
  • 1
    Does this answer your question? Getting path of project, or layer file in PyQGIS?, where you may be interested in this approach: >>>layerProvider = layer.dataProvider() >>>FilePath = layerProvider.dataSourceUri() >>>FilePath = os.path.dirname(unicode(FilePath))
    – Taras
    Commented Feb 7, 2022 at 12:24
  • 1
    I believe that's about right. Pretty similar than to get the data provider and its uri dp = my_lyr.dataProvider() print(dp.uri()).
    – user30184
    Commented Feb 7, 2022 at 12:25

0

Browse other questions tagged or ask your own question.