2

I will be importing some CSV files from the directory of the script that I have opened on Python Console on QGIS. Yet, I cannot access the true directory with os.getcwd() or something else.

Is there a way to do that?

meteergen
  • 121
  • 5
  • Have you tried `Path(__file__).parent`? (`from pathlib2 import Path`) – Danny Varod Dec 10 '20 at 18:17
  • @DannyVarod I have tried. But it gives the wrong path. For instance, my python file is in the desktop. All the methods I try found it in the documents. I think it is because of the QGIS idle and I don't know how to solve it. – meteergen Dec 11 '20 at 07:55

1 Answers1

3

The QGIS python console is an emulated python console (python plugin).

To do that it's a little bit tricky and I didn't find an other way :

import os
from console.console import _console

script_path = _console.console.tabEditorWidget.currentWidget().path
print(os.path.dirname(script_path))

NB :This sample example only works when the tabEditor is opened and the script is saved on the disk.

lejedi76
  • 216
  • 1
  • 5