Get root path of Flask application

后端 未结 2 1429
慢半拍i
慢半拍i 2020-11-28 14:42

I\'m working on a Flask extension from which I want to create a directory in the project\'s root path on the file system.

Suppose we have this directory structure

相关标签:
2条回答
  • 2020-11-28 14:52

    app.root_path is the absolute path to the root directory containing your app code.

    app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

    0 讨论(0)
  • 2020-11-28 14:55

    app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.

    filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt')
    
    0 讨论(0)
提交回复
热议问题