简体   繁体   English

从 QGIS Python 控制台查找 Python 脚本的目录

[英]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.我将从我在 QGIS 上的 Python 控制台上打开的脚本目录中导入一些 CSV 文件。 Yet, I cannot access the true directory with os.getcwd() or something else.但是,我无法使用os.getcwd()或其他方式访问真实目录。

Is there a way to do that?有没有办法做到这一点?

The QGIS python console is an emulated python console (python plugin). QGIS python 控制台是一个模拟的 python 控制台(python 插件)。

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.注意:此示例仅在打开 tabEditor 并将脚本保存在磁盘上时有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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