How to Get the Parent Directory in Python

How do I get the parent directory in Python?

Python 3.4

Use the pathlib module.

from pathlib import Path
path = Path("/here/your/path/file.txt")

Old answer

Try this:

import os
print os.path.abspath(os.path.join(yourpath, os.pardir))

where yourpath is the path you want the parent for.

Get parent of current directory from Python script

Using os.path

To get the parent directory of the directory containing the script (regardless of the current working directory), you'll need to use __file__.

Inside the script use os.path.abspath(__file__) to obtain the absolute path of the script, and call os.path.dirname twice:

from os.path import dirname, abspath
d = dirname(dirname(abspath(__file__))) # /home/kristina/desire-directory

Basically, you can walk up the directory tree by calling os.path.dirname as many times as needed. Example:

In [4]: from os.path import dirname

In [5]: dirname('/home/kristina/desire-directory/scripts/')
Out[5]: '/home/kristina/desire-directory/scripts'

In [6]: dirname(dirname('/home/kristina/desire-directory/scripts/'))
Out[6]: '/home/kristina/desire-directory'

If you want to get the parent directory of the current working directory, use os.getcwd:

import os
d = os.path.dirname(os.getcwd())

Using pathlib

You could also use the pathlib module (available in Python 3.4 or newer).

Each pathlib.Path instance have the parent attribute referring to the parent directory, as well as the parents attribute, which is a list of ancestors of the path. Path.resolve may be used to obtain the absolute path. It also resolves all symlinks, but you may use Path.absolute instead if that isn't a desired behaviour.

Path(__file__) and Path() represent the script path and the current working directory respectively, therefore in order to get the parent directory of the script directory (regardless of the current working directory) you would use

from pathlib import Path
# `path.parents[1]` is the same as `path.parent.parent`
d = Path(__file__).resolve().parents[1] # Path('/home/kristina/desire-directory')

and to get the parent directory of the current working directory

from pathlib import Path
d = Path().resolve().parent

Note that d is a Path instance, which isn't always handy. You can convert it to str easily when you need it:

In [15]: str(d)
Out[15]: '/home/kristina/desire-directory'

How to get the path of the parent directory in python

one way:

import os 

config_file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'configs\config.json')


or (you will need to pip install pathlib):

from pathlib import Path

dir = Path(__file__).parents[1]
config_file_path = os.path.join(dir, 'configs/config.json')


third way:

from os.path import dirname as up

dir = up(up(__file__))

config_file_path = os.path.join(dir, 'configs\config.json')

How to get the parent dir location

You can apply dirname repeatedly to climb higher: dirname(dirname(file)). This can only go as far as the root package, however. If this is a problem, use os.path.abspath: dirname(dirname(abspath(file))).

Importing modules from parent folder

It seems that the problem is not related to the module being in a parent directory or anything like that.

You need to add the directory that contains ptdraft to PYTHONPATH

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.

How do I get the parent directory's name only, not full path?

The simplest way to do this would be using pathlib. Using parent will get you the parent's full path, and name will give you just the last component:

>>> from pathlib import Path
>>> path = Path("/a/b/c/d/e")

For comparison, to do the same with os.path, you will need to get the basename of the dirname of your path. So that translates directly to:

import os

path = "C:/example/folder/file1.jpg"

Which is the nicer version of:


Where both give:


As you can see, the pathlib approach is much clearer and readable. Because pathlib incorporates the OOP approach for representing paths, instead of strings, we get a clear chain of attributes/method calls.

Is read in order as:

start from path -> take its parent -> take its name

Whereas in the os functions-accepting-strings approach you actually need to read from inside-out!


Is read in order as:

The name of the parent of the path

Which I'm sure you'll agree is much harder to read and understand (and this is just a simple-case example).

You could also use the str.split method together with os.sep:

>>> path = "C:\\example\\folder\\file1.jpg"
>>> path.split(os.sep)[-2]

But as the docs state:

Note that knowing this [(the separator)] is not sufficient to be able to parse or
concatenate pathnames — use os.path.split() and os.path.join() — but
it is occasionally useful.

How to get parent folder name of current directory?

You can get the last part of any path using basename (from os.path):

>>> from os.path import basename
>>> basename('/path/to/directory')

Just to note, if your path ends with / then the last part of the path is empty:

>>> basename('/path/to/directory/')

How do I get a file's parent directory?

Use os.path.dirname to get the directory path. If you only want the name of the directory, you can use os.path.basename to extract the base name from it:

>>> path = r'c:\xxx\abc\xyz\fileName.jpg'
>>> import os
>>> os.path.dirname(path)
>>> os.path.basename(os.path.dirname(path))

How do i import a function from a file in a parent directory

in the python structure for import, you should use system layout I mean:


so first of all use init file the simply use import

Related Topics

Leave a reply