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).
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
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.
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)
| Goal | Method | Example |
|---|---|---|
| Get working directory | os.getcwd() or Path.cwd() | /Users/john/project |
| Get script’s location | os.path.dirname(os.path.abspath(__file__)) or Path(__file__).resolve().parent | /Users/john/project/scripts |