4

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)

Vince
  • 20,017
  • 15
  • 45
  • 64
Elio Diaz
  • 3,456
  • 9
  • 22
  • Duplicate, see: https://gis.stackexchange.com/questions/34560/getting-path-of-project-or-layer-file-in-pyqgis – Zoltan Dec 13 '18 at 21:11
  • 3
    This isn't a duplicate - that question refers to getting to path of the project itself, not layers within the project. – ndawson Dec 13 '18 at 21:41

1 Answers1

7

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.

Joseph
  • 75,746
  • 7
  • 171
  • 282