官术网_书友最值得收藏!

Accessing the script path from within your script

Sometimes, you need to know exactly where the current working directory is so that you can access external resources.

Getting ready

This code uses the Python built-in library and can be used in any context. We will run this recipe in the QGIS Python console.

How to do it…

In this recipe, we will get the current working directory of the Python console, which can change with configuration:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. In the Python console, run the following code:
    import os
    os.getcwd()

How it works…

QGIS relies heavily on file system paths to run the application and to manage external data. When writing cross-platform QGIS code, you cannot assume the working directory of your script.

There's more…

On his blog, one of the QGIS developers has an excellent post about the various aspects of path variables in QGIS beyond just the execution directory; you can check it out at http://spatialgalaxy.net/2013/11/06/getting-paths-with-pyqgis/.

主站蜘蛛池模板: 贵州省| 增城市| 蒙城县| 盱眙县| 宿松县| 出国| 聂拉木县| 玉田县| 庄河市| 隆回县| 含山县| 浦东新区| 沾益县| 隆德县| 响水县| 象州县| 石门县| 建水县| 西华县| 武冈市| 庆云县| 百色市| 抚州市| 汉中市| 岳池县| 外汇| 农安县| 长白| 信丰县| 隆化县| 黔南| 赤城县| 惠安县| 石棉县| 沭阳县| 灵宝市| 蓬安县| 裕民县| 息烽县| 镇赉县| 平安县|