How to Get the Current Directory in Python

Ever opened a Python script only to find that your files aren’t being saved where you expected, or your relative paths just don’t work, One common culprit: the “current directory” (also called the working directory) isn’t what you assumed. We’ll walk through python get current directory, what exactly that means, how to use it in a real project, and avoid common pitfalls. By the end you’ll feel confident knowing exactly what “current directory” means in your code and how to work with it reliably.

What Is the “Current Directory” in Python

In simple terms: when your Python program runs, the current working directory (CWD) is the directory from which the program looks up relative file paths.
When you call open('data.csv'), Python looks for data.csv in the CWD (unless you give an absolute path).
However, note: the script file itself may live in a different directory than the CWD (for example if you run it from another folder). So there are two common things you might want:

  • The CWD (where the program is executing).
  • The directory of the script file (where the .py file resides).

How to Python Get Current Directory

Using the os module:

This is the classic way:

import os
cwd = os.getcwd()
print("get current directory:", cwd)

This returns a string with the absolute path to the working directory.
It works, but this method is a bit older style.

Using the pathlib module (modern Python):

In newer Python (3.4+), you can use pathlib, which offers an object-oriented path interface:

from pathlib import Path
cwd = Path.cwd()
print("get current directory:", cwd)

This returns a Path object rather than a string. You can convert to string with str(cwd).
Many developers prefer this for clarity and ease of chaining path operations.

How to Get the Directory of the Script File

Suppose your script has data files in the same folder (or a subfolder), and you want the file location, not necessarily where the user ran it from. Then you can use:

import os
script_dir = os.path.dirname(os.path.abspath(__file__))
print("Script directory:", script_dir)

This retrieves the folder containing the running .py file.

Or with pathlib:

from pathlib import Path
script_dir = Path(__file__).parent.resolve()
print("Script directory:", script_dir)

This is helpful especially in packaged code or when you want relative paths to the script’s folder.

When Things Don’t Work as Expected

CWD vs Script Directory confusion:

If you run a script from another folder (e.g., python /home/user/scripts/my_script.py while your shell is in /home/user), then os.getcwd() returns /home/user, but the script is in /home/user/scripts. Using the wrong one will cause file-not-found errors.

__file__ may not exist:

In some interactive environments (REPL, notebooks) __file__ is not defined, so the script-directory method fails.

Symbolic links and real paths:

If your script is executed via a symlink, os.path.abspath(__file__) may point to the symlink location rather than the “real” file location. Use os.path.realpath() to resolve symbolic links.

Changing directory during execution:

If your script does os.chdir(...), then os.getcwd() changes, and if you rely on CWD, relative paths may break. Be careful when your code modifies the current directory.

Loading a Data File Next to Your Script

Let’s imagine you’re writing a script process_data.py that lives in the folder /projects/mytool/. In the same folder you have a subfolder data with input.csv. You want your code to load input.csv reliably, regardless of where the user runs the script from.

Here’s how you might write that using pathlib:

from pathlib import Path

# get current directory of the script file
script_dir = Path(__file__).parent.resolve()

# define path to data folder
data_file = script_dir / "data" / "input.csv"

# check it exists
if not data_file.exists():
    raise FileNotFoundError(f"Data file not found: {data_file}")

print("Loading data from:", data_file)
with open(data_file, "r") as f:
    # read data...
    pass

Why this works well:

  • script_dir is independent of where the program was launched from.
  • You’re building the path explicitly: script_dir / "data" / "input.csv" ensures cross-platform separators.
  • You check existence before reading.

Contrast that with using os.getcwd(), which would require you to assume the user ran the script from the same folder (which might not happen) and could lead to brittle code.

Conclusion

Knowing python get current directory and understanding which directory you actually need is a powerful skill. Whether working with os.getcwd() (for working directory) or resolving __file__ (for script directory), you now have clarity and confidence to write code that behaves consistently across environments.

Related blog posts