How to Get a Current Directory in Python

Python can show the current working directory with Path.cwd() or os.getcwd(). This guide shows you how to get the folder your script is running from and how to avoid confusing it with the script’s file location.

What you’ll build or solve

You’ll write Python code that prints the current working directory. Done means you can check where Python is looking for files and use that path safely in scripts.

When this approach works best

Getting the current directory works best when:

  • Your script needs to read or write files.
  • You get a “file not found” error and need to check where Python is running.
  • You want to debug paths in a project.
  • You run the same script from different folders.

This is a bad idea if you actually need the folder where the Python file itself is saved. The current working directory and the script directory can be different.

Prerequisites

  • Python installed
  • A code editor or terminal
  • Basic knowledge of running a Python file

No third-party package is required.

Step-by-step instructions

Step 1: Use Path.cwd() to get the current directory

The modern way to get the current working directory is Path.cwd() from the pathlib module.

from pathlib import Path

current_directory = Path.cwd()
print(current_directory)

Path.cwd() returns a Path object. You can print it, join it with file names, or use it in file operations.

Example with a file path:

from pathlib import Path

current_directory = Path.cwd()
data_file = current_directory / "data.txt"

print(data_file)

The / operator joins paths in a clean, readable way.

What to look for

  • Path.cwd() shows the folder Python is currently running from.
  • The result may change depending on where you run the command.
  • A Path object is usually easier to work with than a plain string.
  • Use this when you want to build paths from the current working directory.

Step 2: Use os.getcwd() as an alternative

Python also has the older os.getcwd() function.

import os

current_directory = os.getcwd()
print(current_directory)

os.getcwd() returns a string.

You can use it like this:

import os

current_directory = os.getcwd()
data_file = os.path.join(current_directory, "data.txt")

print(data_file)

This still works and appears in many older codebases.

For new code, pathlib often reads better:

from pathlib import Path

data_file = Path.cwd() / "data.txt"
print(data_file)

Both methods return the current working directory. The main difference is the type of value you get back.

Step 3: Check the script’s folder when needed

The current working directory is not always the same as the folder where your Python file lives.

Use __file__ when you need the script’s own location:

from pathlib import Path

script_directory = Path(__file__).parent
print(script_directory)

This is useful when your script needs a file stored next to it.

Example project:

project/
├── app.py
└── data.txt

Inside app.py:

from pathlib import Path

script_directory = Path(__file__).parent
data_file = script_directory / "data.txt"

print(data_file)

This points to data.txt next to app.py, even if you run the script from another folder.

Use Path.cwd() when you need the folder where Python was started. Use Path(__file__).parent when you need the folder that contains the script file.

Examples you can copy

Example 1: Print the current working directory

from pathlib import Path

print(Path.cwd())

Use this when you need a quick path check.

Example 2: Read a file from the current directory

from pathlib import Path

file_path = Path.cwd() / "notes.txt"

with open(file_path, "r", encoding="utf-8") as file:
    content = file.read()

print(content)

This reads notes.txt from the folder where Python is running.

Example 3: Save a file in the current directory

from pathlib import Path

output_file = Path.cwd() / "result.txt"

with open(output_file, "w", encoding="utf-8") as file:
    file.write("Task complete")

print(f"Saved to {output_file}")

This creates result.txt in the current working directory.

Example 4: Load a file next to the script

from pathlib import Path

script_directory = Path(__file__).parent
config_file = script_directory / "config.json"

print(config_file)

Use this when your script should always look beside itself, not in the folder where the command was run.

Common mistakes and how to fix them

Mistake 1: Confusing current directory with script directory

What you might do:

from pathlib import Path

file_path = Path.cwd() / "config.json"

Why it breaks: If you run the script from a different folder, Python looks for config.json in that folder instead of next to the script.

Correct approach:

from pathlib import Path

file_path = Path(__file__).parent / "config.json"

Use the script directory when the file belongs with the script.

Mistake 2: Hardcoding an absolute path

What you might do:

file_path = "/Users/alex/project/data.txt"

Why it breaks: This path works only on one computer or operating system.

Correct approach:

from pathlib import Path

file_path = Path.cwd() / "data.txt"

Build paths dynamically so your code is easier to move.

Mistake 3: Using string joins for paths

What you might do:

path = os.getcwd() + "/data.txt"

Why it breaks: Manual string paths can break across operating systems or create double slashes.

Correct approach:

from pathlib import Path

path = Path.cwd() / "data.txt"

Let Python build the path.

Troubleshooting

If Python says “No such file or directory,” print Path.cwd() and check where the script is running.

If your file exists but Python cannot find it, check whether you need the script directory instead of the current directory.

If __file__ is not defined, you may be running code in an interactive shell or notebook.

If paths look different in VS Code and the terminal, compare the working directory in both places.

If your code fails on another computer, remove hardcoded absolute paths.

If Windows paths show backslashes, that is normal. pathlib handles them correctly.

Quick recap

  • Use Path.cwd() to get the current working directory.
  • Use os.getcwd() if you need the older string-based method.
  • Use Path(__file__).parent to get the script’s folder.
  • Current directory and script directory can be different.
  • Build file paths with pathlib instead of string concatenation.
  • Print the current directory when debugging file path errors.