Python - Uploading files relative to the project root

My project layout is as follows:

├ ...
├── pve
│   ├── blahblah
│   │   ├── TestDefinition.py
│   │   ├── TestDefinition.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   └── pve.py
├── src
│   └── definitions
│       └── THISFILE.yml
└── ...

I need to be able to extract files (e.g. THISFILE.yml) from src / definitions with the pve / blahblah / TestDefinition.py class.

How to access the root of the project? Once I do this, I can access the .yml files. TIA.

+5
source share
1 answer

I like to create some kind of configuration file in the root of the project, which has the aboslute path to the root. I do this only because the framework that I usually use (django, scrapy) has a kind of convention like this

├ ...
├── pve
│   ├── blahblah
│   │   ├── TestDefinition.py
│   │   ├── TestDefinition.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   └── pve.py
├── src
│   └── definitions
│       └── THISFILE.yml
└── settings.py



# settings.py

import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DEFINITIONS_ROOT = os.path.join(PROJECT_ROOT, 'src', 'definitions')


from myproject import settings 
settings.DEFINITIONS_ROOT
+10
source

All Articles