user
How to get current path in Python?
alphonsio

The best way to get current working directory in Python is to use os.getcwd():

import os
current_path = os.getcwd()

In Python, you can get the current working directory (path) in several ways — depending on what exactly you need (the script’s directory vs. where it’s run from).

1. Get the current working directory

This is the directory from which your Python script is being executed.

import os

current_path = os.getcwd()
print(current_path)

Example output:

/Users/john/projects/myapp

2. Get the directory of the current script file

If your script is located in /path/to/script.py, you can get that path with:

import os

script_dir = os.path.dirname(os.path.abspath(__file__))
print(script_dir)

Example output:

/Users/john/projects/myapp/scripts

⚠️ __file__ won’t work in interactive sessions like the Python REPL or Jupyter Notebook — only in actual .py scripts.


3. Using pathlib (modern, cleaner syntax)

from pathlib import Path

current_path = Path.cwd()
print(current_path)

Or, to get the current script’s directory:

from pathlib import Path

script_dir = Path(__file__).resolve().parent
print(script_dir)

Summary

GoalMethodExample
Get working directoryos.getcwd() or Path.cwd()/Users/john/project
Get script’s locationos.path.dirname(os.path.abspath(__file__)) or Path(__file__).resolve().parent/Users/john/project/scripts