DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Get the Path and Directory of a File in Python with __file__ and pathlib

Get a Python module’s directory with pathlib, distinguish it from the working directory, and learn when __file__ or resolve() may not behave as expected.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a file-backed Python module, use Path(__file__).parent to get its containing directory. Use Path(__file__).resolve().parent when you need an absolute path with symlinks resolved and .. components removed. These paths are anchored to the module, not the process’s current working directory.

Get the directory containing the current Python file

Use the standard-library pathlib module:

from pathlib import Path

module_dir = Path(__file__).parent

parent returns the directory component of the path as represented. It is a lexical operation: it does not inspect the filesystem, resolve symlinks, or remove .. components. [Python pathlib documentation]

As an Amazon Associate I earn from qualifying purchases.

Choose the path operation you need

What you need Use What it does
The containing directory as represented by the module path Path(__file__).parent Returns the lexical parent; it does not resolve symlinks or normalize ... [Python pathlib documentation]
An absolute path, without resolving symlinks Path(__file__).absolute() Makes the path absolute without normalization or symlink resolution. [Python pathlib documentation]
An absolute path with symlinks resolved and .. removed Path(__file__).resolve().parent resolve() makes the path absolute, follows symlinks, and removes .. components. [Python pathlib documentation]
The process’s current working directory Path.cwd() Returns the current working directory, which is distinct from the directory containing a module. [Python pathlib documentation]

Build a path to a nearby resource

To locate a file stored beside the current module, join its name to the module directory with the / operator:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from pathlib import Path

module_dir = Path(__file__).resolve().parent
config_file = module_dir / "settings.json"

Here, module_dir is based on the module’s pathname; it does not depend on where the process was launched. If you want to refer directly to a file with the same parent as the module, use Path(__file__).resolve().with_name("settings.json"). [Python pathlib documentation]

Why the current working directory is different

Path.cwd() answers where the process is running, not where a particular Python source file is stored. A relative path is generally interpreted from the current working directory. As the Python documentation explains, “Relative paths are interpreted relative to the current working directory, not the directory of the Path object.” [Python pathlib documentation]

Use Path.cwd() when you need the process’s working directory. Use a path derived from __file__ when the resource is intended to be located beside a module.

When __file__ may not be available

__file__ is module metadata, not a universal Python global. The Python 3.14.8 data model describes it as optional: when present, it identifies the pathname from which the module was loaded, but some module types or loaders without a meaningful pathname may not provide it. Code that can run in such a context should be given or derive an appropriate base path instead of assuming __file__ exists. [Python data model documentation]

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Using module-relative paths in a PyInstaller bundle

PyInstaller 6.8.0 documents that its bootloader sets __file__ for bundled modules and recommends resolving it to locate sibling data files. Its documented pattern is Path(__file__).resolve().with_name("other-file.dat"). [PyInstaller runtime information]

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What resolve() does—and does not—guarantee

Path.resolve() makes a path absolute, resolves symlinks, and removes .. components. With non-strict behavior, it resolves as far as possible and appends any remaining path components without checking that they exist. A resolved path therefore should not be treated as proof that its target exists. [Python pathlib documentation]

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.