Sunday, 24 March 2024

Get cache-basepath in python: get_cache_path

from pathlib import Path
import os

def get_cache_path(app_name: str) -> Path:
    """Return the path for the app's cache directory, automatically adjusted for the OS.

    Args:
        app_name (str): The name of the application.

    Returns:
        Path: The absolute path to the app's cache directory.
    """
    if os.name == 'nt':  # Windows
        cache_dir = Path(os.getenv('LOCALAPPDATA', Path.home())) / app_name / "Cache"
    else:
        cache_dir = Path.home() / ".cache" / app_name
    if not cache_dir.exists():
        cache_dir.mkdir(parents=True)
    return cache_dir

# Example usage
cache_path = get_cache_path("my_app")
print(cache_path)

No comments:

Post a Comment

Parse Wikipedia dump

""" This module processes Wikipedia dump files by extracting individual articles and parsing them into a structured format, ...