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:
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:
If you start from a relative path, make it absolute first:
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 ..:
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:
Result:
With a directory path that ends in a slash, normalize it first:
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:
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.
Related reading
- Using Python''s PIL, how do I enhance the contrast/saturation of an image?
- Using .replace at a specific index
- Using scikit-learn DecisionTreeClassifier to cluster
- Using TensorFlow through Jupyter Python 3
- Using tf.tile to replicate a tensor N times
- Using the predict_proba function of RandomForestClassifier in the safe and right way
- Using 'try' vs. 'if' in Python
- Using WeightedRandomSampler in PyTorch
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.