0

I want to export my polygon with this file name : yyyymmdd_PVD.gpkg

from qgis import processing
import datetime
    
dir = 'C:/Users/CMCA/Downloads/'+ str(datetime.datetime.now())+'.gpkg'
processing.run("native:savefeatures", 
               {'INPUT':iface.activeLayer(),
               'OUTPUT':dir,
               'LAYER_NAME':'test',
               'DATASOURCE_OPTIONS':'',
               'LAYER_OPTIONS':''})

Error:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 61, in <module>
  File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\tools\general.py", line 108, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\core\Processing.py", line 187, in runAlgorithm
    ret, results = execute(alg, parameters, context, feedback, catch_exceptions=False)
  File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\gui\AlgorithmExecutor.py", line 72, in execute
    results, ok = alg.run(parameters, context, feedback, {}, False)
_core.QgsProcessingException: Could not create layer C:/Users/CMCA/Downloads/2023-03-21 17:19:46.771813.gpkg: Unable to load C:/Users/CMCA/Downloads/2023-03-21 17 provider

2 Answers 2

3

You are importing the datetime module with import datetime but then you try to use a now() method from a date object/module which does not exist.

You probably meant to use datetime.datetime.now().

1
  • I changed it , but it still get an error
    – charliey
    Commented Mar 21, 2023 at 9:22
2

You need to change the date special characters to meet prerequisites for creating folder structures in Windows.

You cannot create folders with characters like : or /

Another problem you may face, is that the word dir is reserved in Python, avoid using it as a variable name

Format the date without the character :

from qgis import processing
from datetime import datetime

date = datetime.strftime(datetime.now(), "%Y_%m_%d_%H_%M_%S") # or "%Y%m%d_%H%M%S"
    
archive= f'C:/Users/CMCA/Downloads/{date}.gpkg'
processing.run("native:savefeatures", 
               {'INPUT':iface.activeLayer(),
               'OUTPUT':archive,
               'LAYER_NAME':'test',
               'DATASOURCE_OPTIONS':'',
               'LAYER_OPTIONS':''})
6
  • Thank you for this! it works but one more thing , if i load it, it saves on my downloads.yest but it is not automatically loaded in my qgis , how can i do that ?
    – charliey
    Commented Mar 22, 2023 at 3:08
  • you are using the processing.run tool, for this it only processes, what you can do is change to the processing.runAndLoadResults tool Commented Mar 22, 2023 at 10:31
  • flag the answer as correct to help another users. Commented Mar 22, 2023 at 10:31
  • To learn more, see: docs.qgis.org/3.28/en/docs/user_manual/processing/console.html Commented Mar 22, 2023 at 10:38
  • 1
    Hello Charlie, for this change it is necessary to include editing with the new code for me to understand what you are trying to do. Remember this is a Q&A forum, for a question an answer, for another question a new post Commented Mar 29, 2023 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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