跨平台方法在Python中获取用户的配置主目录? [英] Cross-platform method to obtain the user's configuration home directory in Python?

查看:71
本文介绍了跨平台方法在Python中获取用户的配置主目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要存储一些配置文件.主要的操作系统似乎有指定的位置来放置这些操作系统.例如,在符合Freedesktop.org的系统上,它将是存储在 $ XDG_CONFIG_HOME 环境变量中的路径.

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.

是否有一种方法(或库)可在主要操作系统(Windows,OS X,Linux)上获取此配置主目录?

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

推荐答案

import os

print os.path.expanduser("~/.my_config/data")

将始终允许您编写...最好将配置文件存储在您想要的位置

will always allow you to write ... config files are best stored where you want them

通常在Windows上是

oftentimes on windows this is

os.path.expandvars("%appdata%/.my_config")

大多数其他系统倾向于将用户配置文件导演(〜)

most other systems tend to put the in the userprofile director (~)

除非您要查找特定的配置文件位置(可能要与其他软件进行交互……在这种情况下,我们需要知道哪个软件)

unless you are looking for a specific configuration file location(perhaps to interact with another software... in which case we would need to know which software)

这篇关于跨平台方法在Python中获取用户的配置主目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆