Skip to content

Instantly share code, notes, and snippets.

@wcDogg
Last active August 30, 2023 16:26
Show Gist options
  • Save wcDogg/e01ad5d96644b7bfc01c4162388d6c46 to your computer and use it in GitHub Desktop.
Save wcDogg/e01ad5d96644b7bfc01c4162388d6c46 to your computer and use it in GitHub Desktop.
How to locate OS-appropriate directories for storing app data on a user's computer in Python.

Python: Locate OS Paths for Common App Directories

appdirs helps you locate OS-appropriate directories for storing app data on a user's computer - user, site, caxhe, log. I use appdirs to write log files.

python -m pip install appdirs

appdirs Paths

appname = "SuperApp"
appauthor = "Acme"

user_data_dir(appname, appauthor)
# Mac OS X:   /Users/trentm/Library/Application Support/SuperApp 
# Windows 7:  C:\Users\trentm\AppData\Local\Acme\SuperApp
# Windows 11: C:\Users\trentm\AppData\Local\Acme\SuperApp     
# Linux:      /home/trentm/.local/share/SuperApp

site_data_dir(appname, appauthor)
# Mac OS X:   /Library/Application Support/SuperApp
# Windows 7:  C:\Users\trentm\AppData\Roaming\Acme\SuperApp
# Windows 11: C:\ProgramData\Acme\SuperApp     
# Linux:      /usr/local/share/SuperApp

user_cache_dir(appname, appauthor)
# Mac OS X:   /Users/trentm/Library/Caches/SuperApp
# Windows 7:  C:\Users\trentm\AppData\Local\Acme\SuperApp\Cache
# Windows 11: C:\Users\trentm\AppData\Local\Acme\SuperApp\Cache     
# Linux:     /home/trentm/.cache/SuperApp

user_log_dir(appname, appauthor)
# Mac OS X:   /Users/trentm/Library/Logs/SuperApp
# Windows 7:  C:\Users\trentm\AppData\Local\Acme\SuperApp\Logs
# Windows 11: C:\Users\trentm\AppData\Local\Acme\SuperApp\Logs    
# Linux:      /home/trentm/.cache/SuperApp/log

Using appdirs for Log Files

# confiure.py

import appdirs
from pathlib import Path

#
# Project directory
DIR_PROJ = Path(__file__).parent
DIR_PACK  = Path(__file__).parent.parent

#
# App info
APP_NAME = 'SuperApp'
APP_AUTHOR = 'Acme' 

#
# appdirs
DIR_USER_DATA  = appdirs.user_data_dir(APP_NAME, APP_AUTHOR)
DIR_SITE_DATA  = appdirs.site_data_dir(APP_NAME, APP_AUTHOR)
DIR_USER_CACHE = appdirs.user_cache_dir(APP_NAME, APP_AUTHOR)
DIR_USER_LOG   = appdirs.user_log_dir(APP_NAME, APP_AUTHOR)

# 
# Log files
LOG_DIR = Path(appdirs.user_log_dir(APP_NAME, False))
LOG_PATH_INFO = Path(LOG_DIR / 'info.log') 
LOG_PATH_DEBUG = Path(LOG_DIR / 'debug.log') 
LOG_PATH_WARN = Path(LOG_DIR / 'warning.log')
LOG_PATH_ERROR = Path(LOG_DIR / 'error.log')
LOG_PATH_CRIT = Path(LOG_DIR / 'critical.log')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment