Locate Script Directory

1.1.1 · active · verified Sun Apr 12

The `locate` library provides simple utilities to find the file location of your current running Python script and manage paths. Currently at version 1.1.1, it receives regular updates, with recent releases adding features like context managers and addressing developer tooling concerns.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `this_dir()` to get the path of the executing script and how to use the `chdir` context manager to temporarily change the current working directory. The `chdir` context manager ensures the directory is restored automatically upon exiting the `with` block.

import os
from locate import this_dir, chdir

# Get the directory of the current script
current_script_dir = this_dir()
print(f"Current script directory: {current_script_dir}")

# Using the chdir context manager
original_cwd = os.getcwd()
print(f"Original CWD: {original_cwd}")

with chdir(current_script_dir):
    print(f"CWD inside context: {os.getcwd()}")
    # Perform operations relative to current_script_dir

print(f"CWD after context: {os.getcwd()}") # Should revert to original_cwd

view raw JSON →