python get directory two levels up
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting a directory two levels up in Python is simple once you decide what "current directory" means. Sometimes you want two levels above the process working directory, and other times you want two levels above the script file itself.
Two Different Starting Points
Developers often mix up these two cases:
- '
os.getcwd()orPath.cwd()refers to the current working directory of the running process' - '
__file__refers to the location of the current script file'
Those are not always the same. If you run a script from a different directory, cwd and __file__ can point to different places.
Use pathlib For New Code
pathlib is usually the clearest option.
parents[0] is one level up, and parents[1] is two levels up.
If you want two levels above the script file instead of the working directory, use __file__.
Using .resolve() is useful because it turns relative paths into absolute ones and follows symlinks where appropriate.
The Equivalent os.path Version
If you are working in older code, os.path.dirname() does the same job.
For the script location:
This is completely fine, but pathlib is easier to read once paths become more complex.
Build Paths Relative To That Directory
Often you do not just want the directory name; you want a file under it.
This is cleaner than manually joining string fragments or writing path separators by hand.
Guard Against Shallow Paths
One subtle issue is that not every path has two parents. For example, a path near the filesystem root may not.
That matters in reusable utilities or tests where the path depth may vary.
Why .. Strings Are Not Always Best
You can also write:
This works, but it is less explicit than using .parents. The intent of "two levels up" is clearer when expressed directly.
Wrap It In A Helper When The Pattern Repeats
If several modules need the same logic, put it in one helper and make the starting path explicit.
That keeps call sites readable and avoids repeating nested dirname or parents expressions across the codebase.
Common Pitfalls
The most common mistake is using os.getcwd() when the real requirement is "relative to this file." Those are different questions.
Another mistake is assuming __file__ is always available. It exists in normal scripts and modules, but interactive sessions may not define it.
Developers also sometimes index the wrong parent. In pathlib, parents[0] is one level up, not the original path.
Finally, avoid hard-coded path separators or string slicing. Let pathlib or os.path handle platform-specific details.
Summary
- Decide whether you mean the working directory or the script location.
- Prefer
pathlibfor readable path manipulation. - Use
Path.cwd().parents[1]for two levels above the working directory. - Use
Path(__file__).resolve().parent.parents[1]for two levels above the script directory. - Check path depth when writing reusable helpers.
Related reading
- Python geventbottle. Querying an API. How to use gevent to prevent timeout locks?
- Python glob multiple filetypes
- Python Graph Library
- Python, how to determine if an object is iterable?
- Python how to determine if an object is iterable?
- Python How to find Accuracy Result in SVM Text Classifier Algorithm for Multilabel Class
- Python How to group a list of objects by their characteristics or attributes?
- python how to identify if a variable is an array or a scalar
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.