PyQGIS – How to Retrieve Layer File Path in QGIS 3

pyqgisqgis-3

I've been trying to list al file paths in my project using PyQgis (I'm not really acquainted with it); my goal is to translate and clip them all to export them to a folder
I've tried with:

names =    [layer.name() for layer in QgsProject.instance().mapLayers().values()]

But that only returns the names on the layer panel, I want the full path (and to export it as a csv would be great)

Best Answer

You could use something like the following to get the layer path sources and write the results to a csv:

import csv
layer_paths = [layer.source() for layer in QgsProject.instance().mapLayers().values()]
csv_path = 'path/to/output.csv'

with open(csv_path, 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    for paths in layer_paths:
        writer.writerow([paths])

Tested on QGIS 3.4.2 on Win7 64-bit.

Related Question