Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-platform method to obtain the user's configuration home directory in Python?

My program needs to store some configuration files. The major operating systems seems to have a designated location to place those; for instance, on Freedesktop.org compliant systems, it will be the path stored in the $XDG_CONFIG_HOME environment variable.

Is there a method (or a library) to obtain this configuration home directory across the major operating systems: Windows, OS X, Linux?

like image 338
Dun Peal Avatar asked Nov 03 '14 20:11

Dun Peal


People also ask

What's the process to get the home directory using in Python?

expanduser() function in Python provides the most straightforward method for retrieving the user home directory across all platforms. The Python os module offers os. path. expanduser(") to retrieve the home directory.

What is user's home directory?

A home directory is the directory or folder commonly given to a user on a network or Unix or Linux variant operating system. With the home directory the user can store all their personal information, files, login scripts, and user information.


1 Answers

You can use the package appdirs. This is developed by ActiveState who must have quite a lot of experience on cross-platform python.

import appdirs
appdirs.user_config_dir(appname='MyApp')

The package is just a single file (/module), so if you need it for a small script it is simple to just copy what you need. Otherwise the package is available with both pip and conda.

like image 194
Janus Avatar answered Sep 22 '22 19:09

Janus