djangopython-3.xpathsettingsenvironment

Django: BASE_DIR not correct after the os.path.join(...) command


I am getting the following behavior in Django:

BASE_DIR seems to change when I use the "os.path.join(...)" command on it.

My settings.py file:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

In the Python Shell:

>>> import os
>>> from django.conf import settings

>>> base_dir = settings.BASE_DIR
***'C:\\Users\\gille\\timeless_wisdom'***

>>> file_path = os.path.join(base_dir, '/timeless_wisdom/UserData')
***'C:/timeless_wisdom/UserData'***

So: when I join a relative path with BASE_DIR, I don't get the expected result, but he starts from C:/ again...

I have tried the following, but same result.:

file_path = os.path.join(base_dir, '\\timeless_wisdom\\UserData')

I have tried using PROJECT_ROOT instead of BASE_DIR, but same result.

Anything I am missing? Thanks...


Solution

  • You shouldn't have any leading slashes on the path.

    file_path = os.path.join(base_dir, 'timeless_wisdom/UserData')