python
directory navigation
file system
os module
python tips

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.

Browse interview questions

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() or Path.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.

python
1from pathlib import Path
2
3current = Path.cwd()
4two_up = current.parents[1]
5
6print(current)
7print(two_up)

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__.

python
1from pathlib import Path
2
3script_dir = Path(__file__).resolve().parent
4two_up_from_script = script_dir.parents[1]
5
6print(two_up_from_script)

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.

python
1import os
2
3current = os.getcwd()
4two_up = os.path.dirname(os.path.dirname(current))
5
6print(two_up)

For the script location:

python
1import os
2
3script_path = os.path.abspath(__file__)
4script_dir = os.path.dirname(script_path)
5two_up = os.path.dirname(os.path.dirname(script_dir))
6
7print(two_up)

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.

python
1from pathlib import Path
2
3config_file = Path(__file__).resolve().parent.parents[1] / "config" / "app.yml"
4print(config_file)

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.

python
1from pathlib import Path
2
3path = Path("/tmp")
4if len(path.parents) >= 2:
5    print(path.parents[1])
6else:
7    print("Not enough parent directories")

That matters in reusable utilities or tests where the path depth may vary.

Why .. Strings Are Not Always Best

You can also write:

python
1from pathlib import Path
2
3path = (Path.cwd() / ".." / "..").resolve()
4print(path)

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.

python
1from pathlib import Path
2
3
4def two_levels_up(path: str | Path) -> Path:
5    return Path(path).resolve().parents[1]
6
7
8print(two_levels_up(__file__))
9print(two_levels_up(Path.cwd()))

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 pathlib for 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
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
Track 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.

Browse interview questions

All Rights Reserved.