HOWTO
How to Get the Current Script File Directory in Python
Learn how to get a Python script directory with os.path and pathlib, distinguish it from the working directory, and understand when __file__ is unavailable in interactive sessions and notebooks.
On this page
To get the directory containing a running Python script, use os.path.dirname(os.path.abspath(__file__)) or Path(__file__).resolve().parent. Both start with __file__, the path associated with the script, and derive its containing directory. Use that directory to build paths to files stored beside the script.
Get the directory of the current script
The directory of a script is the folder that contains its .py file. __file__ provides the script path; applying os.path.abspath() and then os.path.dirname() produces an absolute directory path. This is useful when a script needs a nearby configuration, input, or data file. If __file__ is relative, os.path.abspath() resolves it using the process working directory, so the result can depend on where Python was launched. When __file__ is already absolute, that working directory does not determine its location.
Run this example from a saved Python file:
import os
script_path = os.path.abspath(__file__)
script_directory = os.path.dirname(script_path)
directory, filename = os.path.split(script_path)
print("Script directory:", script_directory)
print("Directory and filename:", directory, filename)
Script directory: /work
Directory and filename: /work case-0.py
os.path.abspath() makes the script path absolute using the current process context, and os.path.dirname() extracts the directory portion. os.path.split() returns the directory and filename as separate values. The printed directory will depend on where you save the file, so there is no universal literal output to copy.
Distinguish the script directory from the working directory
The working directory is the process location used to resolve relative paths, such as open("data.csv"). Check it with os.getcwd(). It may be the terminal’s current folder, an IDE setting, or a directory selected by a parent process. It is not necessarily the folder containing the script.
By contrast, __file__ refers to the path of the Python file being run. If a script in /projects/tool is launched while the working directory is /tmp, a relative open of data.csv is resolved from /tmp; a path built from the script directory points into /projects/tool. Choose the base that matches the file’s intended location. For a file kept beside the script, join its name to the derived script directory with os.path.join(script_directory, "data.csv").
An absolute path derived from __file__ is not a guarantee that a file exists, nor does it resolve every symbolic-link or deployment concern. It identifies a location to use as a base; check or open the resulting path according to your application’s needs.
Use os.path to get the script directory
The example above keeps the intermediate values so each operation is clear. If only the containing directory is needed, combine the calls as os.path.dirname(os.path.abspath(__file__)). If both parts are useful, keep the result of os.path.split() instead of discarding the filename.
To construct a path to a sibling file, use os.path.join() rather than concatenating strings with a slash. This lets Python apply the platform’s path separator rules. For example, os.path.join(script_directory, "settings.json") creates a path rooted at the script directory. A relative filename passed directly to open() is instead resolved from the working directory.
Use pathlib to get the script directory
pathlib expresses the same operation with path objects. In a saved script, Path(__file__).resolve().parent is the containing directory: resolve() obtains an absolute, resolved path, and .parent selects its parent folder. Use / to append a child path, for example script_directory / "settings.json", and pass the resulting Path to APIs that accept path-like objects, including open().
The equivalent expression is Path(__file__).resolve().parent; import Path from pathlib before using it. Prefer pathlib when the surrounding code already uses Path objects, and os.path when working with string paths or existing APIs that expect them. Neither approach changes the process working directory.
Handle notebooks and other execution contexts
__file__ is normally defined when Python runs a saved script, but interactive interpreters and notebook kernels may not define it. In those contexts, the script-based expressions above cannot identify a notebook file in a portable way. os.getcwd() still reports the process working directory, which can help locate files relative to the kernel’s current location, but it is not a substitute for a notebook’s source-file path.
For a file that is not the running script, start from that file’s known path instead of __file__, then use os.path.dirname() or the Path object’s .parent to get its directory. Keep the path source explicit so later readers can tell whether it represents the script, another file, or the process working directory.