Using Python’s os.path, how do I go up one directory?

Using Python’s os.path, how do I go up one directory?

I recently upgrade Django from v1.3.1 to v1.4.
In my old I have 
    os.path.join(os.path.dirname( __file__ ), 'templates').replace('\\', '/'),
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

This will point to /Users/hobbes3/Sites/mysite/templates, but because Django v1.4 moved the project folder to the same level as the app folders, my file is now in /Users/hobbes3/Sites/mysite/mysite/ instead of /Users/hobbes3/Sites/mysite/.
So actually my question is now twofold:

How do I use os.path to look at a directory one level above from __file__. In other words, I want /Users/hobbes3/Sites/mysite/mysite/ to find /Users/hobbes3/Sites/mysite/templates using relative paths.
Should I be keeping the template folder (which has cross-app templates, like admin, registration, etc.) at the project /User/hobbes3/Sites/mysite level or at /User/hobbes3/Sites/mysite/mysite?


Answer 1:

os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'templates'))

As far as where the templates folder should go, I don’t know since Django 1.4 just came out and I haven’t looked at it yet. You should probably ask another question on SE to solve that issue.

You can also use normpath to clean up the path, rather than abspath. However, in this situation, Django expects an absolute path rather than a relative path.

For cross platform compatability, use os.pardir instead of '..'.

Answer 2:

To get the folder of a file just use:


To get a folder up just use os.path.dirname again


You might want to check if __file__ is a symlink:

if os.path.islink(__file__): path = os.readlink (__file__)

Answer 3:

You want exactly this:

BASE_DIR = os.path.join( os.path.dirname( __file__ ), '..' )

Answer 4:

If you are using Python 3.4 or newer, a convenient way to move up multiple directories is pathlib:

from pathlib import Path

full_path = "path/to/directory"
str(Path(full_path).parents[0])  # "path/to"
str(Path(full_path).parents[1])  # "path"
str(Path(full_path).parents[2])  # "."

Answer 5:

Personally, I’d go for the function approach

def get_parent_dir(directory):
    import os
    return os.path.dirname(directory)

current_dirs_parent = get_parent_dir(os.getcwd())

Answer 6:

I think the easiest thing to do is just to reuse dirname()
So you can call

os.path.dirname(os.path.dirname( __file__ ))

if you file is at /Users/hobbes3/Sites/mysite/templates/

This will return “/Users/hobbes3/Sites/mysite”

Answer 7:

from os.path import dirname, realpath, join
join(dirname(realpath(dirname(__file__))), 'templates')


If you happen to “copy” through symlinking, @forivall’s answer is better:


   -> ~user/project1/

The method above will ‘see’ wrong.html while @forivall’s method will see right.html

In the absense of symlinks the two answers are identical.

Answer 8:

This might be useful for other cases where you want to go x folders up. Just run walk_up_folder(path, 6) to go up 6 folders.

def walk_up_folder(path, depth=1):
    _cur_depth = 1        
    while _cur_depth < depth:
        path = os.path.dirname(path)
        _cur_depth += 1
    return path   

Answer 9:

For a paranoid like me, I’d prefer this one

    __file__.rsplit('/', 2)[0] + '/templates',

Answer 10:

To go n folders up… run up(n)

import os

def up(n, nth_dir=os.getcwd()):
    while n != 0:
        nth_dir = os.path.dirname(nth_dir)
        n -= 1
    return nth_dir

Answer 11:

Of course: simply use os.chdir(..).