from qgis.core import QgsProject # create a new QGIS project project = QgsProject.instance() project.setFileName('/path/to/new/project.qgs') project.write() # load an existing project file project.read('/path/to/project.qgs') # get project information title = project.title() crs = project.crs() extent = project.extent()
from qgis.core import QgsProject from qgis.PyQt.QtCore import QFileInfo # check whether a project file exists file_info = QFileInfo('/path/to/project.qgs') if file_info.exists(): # read the project file project = QgsProject.instance() project.read('/path/to/project.qgs') else: print('Project file does not exist!')In this example, we check whether the project file `/path/to/project.qgs` exists. If it does, we load it into the QgsProject instance. If the file does not exist, we print an error message. This can be useful for preventing errors when attempting to load a project file that does not exist.