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

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/.

主站蜘蛛池模板: 沾化县| 垦利县| 曲沃县| 上蔡县| 民权县| 钟山县| 广汉市| 岗巴县| 虹口区| 溧水县| 砀山县| 苗栗县| 南康市| 南汇区| 温州市| 和田市| 古丈县| 铜鼓县| 古丈县| 南陵县| 铜山县| 崇左市| 大石桥市| 桦南县| 宁夏| 霍邱县| 庄浪县| 仲巴县| 防城港市| 治多县| 永福县| 龙里县| 海原县| 垦利县| 启东市| 延吉市| 屯留县| 托克托县| 越西县| 秭归县| 金华市|