简体   繁体   中英

Finding directory of Python script from QGIS Python Console

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM