Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to get the path and the file name of the current project from a python script? I'm currently trying to develop a python plugin for qgis and I need to create a file with a similar name in the same folder.

share|improve this question
up vote 10 down vote accepted

You can use the following method from the QgsProject class to retrieve the path to the project file:

from qgis.core import *
prjpath = QgsProject.instance().fileName()

If there is no project loaded, or the current project has not yet been saved, this will return an empty QString:


You can convert the project file path further to a Python Unicode string (if needed) with:


However, if you intend to have your plugin be cross-platform, you are better off not working with the file path as a string in Python, but leverage Qt's excellent QFileInfo class:

from PyQt4.QtCore import *
from qgis.core import *

prjfi = QFileInfo(QgsProject.instance().fileName())
prjfi.absolutePath()  # what you are probably looking for

The QFileInfo class (along with QFile and QDir) will handle many cross-platform filesystem issues for you, and inclusively provide methods that would otherwise be spread across more Python classes.

share|improve this answer
Great! That does it! Thanks! – Dominik Froehlich Sep 24 '12 at 9:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.