Using Python's os.path, how do I go up one directory?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To go up one directory with Python's os.path, the most common tool is os.path.dirname(). It returns the parent portion of a path string, and when combined with os.path.abspath() it gives you a reliable parent directory for relative or current paths.
Core Sections
Basic Parent Directory Example
If you already have a concrete path string, os.path.dirname() removes the last path component.
If the path points to a directory rather than a file, the same rule applies: dirname() returns everything before the final path segment.
From the Current Working Directory
To move up one level from the current working directory, first get the absolute current path, then take its directory name.
That is usually what people mean when they ask how to "go up one directory."
If your code starts from a relative path, normalize it first:
Using abspath() makes the result predictable, because the parent is computed from a full path instead of whatever relative string happened to be passed in.
Another Option: os.path.join(..., os.pardir)
You can also build a parent path explicitly with os.pardir, which is the string representing the parent marker, usually ...
This is handy when you want to move up several levels by chaining os.pardir values or combining them with other path construction logic.
For one level, dirname() is usually simpler. For relative navigation logic, join(..., os.pardir) can be more expressive.
File Paths Versus Directory Paths
A common source of confusion is whether the input path names a file or a directory.
Example with a file:
Result:
Example with a directory path that ends in a slash:
Using os.path.normpath() can help remove trailing separators before taking the parent, which makes intent clearer in edge cases.
When pathlib Might Be Better
Even though the question is about os.path, it is worth knowing that modern Python code often uses pathlib because it reads more cleanly.
Still, if you are already working in an os.path codebase, dirname() remains a perfectly valid and portable solution.
Another common real-world case is moving up from the current script location rather than from the process working directory. In that situation, start from __file__, convert it to an absolute path, and then call dirname() so the result is based on the script's location instead of wherever the program was launched from.
Common Pitfalls
- Calling
dirname()on relative paths and expecting stable absolute results. - Forgetting about trailing separators when the input path might end with a slash.
- Confusing path-string computation with actually changing the current working directory.
- Repeating parent navigation without normalizing the path and then getting surprising outputs.
- Forgetting that filesystem roots stop upward navigation no matter how many times you ask for the parent.
Summary
- Use
os.path.dirname(path)to get the parent directory of a path. - Combine it with
os.path.abspath()when starting from a relative path. - '
os.path.join(path, os.pardir)is another valid way to express parent navigation.' - Normalize trailing slashes when path shape might be inconsistent.
- Computing a parent path is different from changing the current working directory.

