Python
os.path
directory navigation
file management
programming tips

Python os.path, how to go up one directory (parent folder)

Codemia Knowledge Hub ·

To go up one directory with os.path, call os.path.dirname(). It returns the parent portion of a path string, and combined with os.path.abspath() it gives a reliable parent directory for relative or current paths.

Getting the parent of a path with dirname()

Given a concrete path string, os.path.dirname() drops the last path component:

python
1import os
2
3path = "/Users/markqian/projects/demo/file.txt"
4parent = os.path.dirname(path)
5
6print(parent)
text
/Users/markqian/projects/demo

The same rule applies to a directory path: dirname() returns everything before the final segment.

Going up from the current working directory

Get the absolute current path, then take its directory name. This is usually what "go up one directory" means:

python
1import os
2
3current_dir = os.getcwd()
4parent_dir = os.path.dirname(current_dir)
5
6print("Current:", current_dir)
7print("Parent:", parent_dir)

If you start from a relative path, make it absolute first:

python
1import os
2
3relative_path = "reports/2026/march"
4absolute_path = os.path.abspath(relative_path)
5parent_dir = os.path.dirname(absolute_path)
6
7print(parent_dir)

abspath() makes the result predictable, because the parent is computed from a full path rather than whatever relative string was passed in.

To go up from the script's own location rather than the directory the program was launched from, start from __file__, make it absolute, then call dirname().

Using os.pardir with os.path.join

os.pardir is the parent marker string, usually ..:

python
1import os
2
3path = "/Users/markqian/projects/demo"
4parent = os.path.abspath(os.path.join(path, os.pardir))
5
6print(parent)

Chaining os.pardir values moves up several levels, or you can combine them with other path construction. For a single level, dirname() is usually simpler; for relative navigation logic, join(..., os.pardir) can be more expressive.

File paths vs. directory paths with trailing slashes

With a file:

python
1import os
2
3file_path = "/tmp/example/data.csv"
4print(os.path.dirname(file_path))

Result:

text
/tmp/example

With a directory path that ends in a slash, normalize it first:

python
1import os
2
3directory_path = "/tmp/example/"
4print(os.path.dirname(os.path.normpath(directory_path)))

os.path.normpath() removes the trailing separator before taking the parent, which keeps the intent clear in edge cases.

The pathlib equivalent

Modern Python code often uses pathlib because it reads more cleanly:

python
1from pathlib import Path
2
3path = Path("/Users/markqian/projects/demo/file.txt")
4print(path.parent)

In an existing os.path codebase, dirname() remains a perfectly valid and portable solution.

Mistakes with parent directories

  • Calling dirname() on relative paths and expecting stable absolute results.
  • Forgetting trailing separators when the input path might end with a slash.
  • Confusing computing a parent path string with actually changing the current working directory.
  • Repeating parent navigation without normalizing the path, then getting surprising output.
  • Forgetting that the filesystem root stops upward navigation no matter how many times you ask for the parent.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms