从python中与操作系统无关的地方加载配置文件 [英] Loading a config file from operation system independent place in python

查看:24
本文介绍了从python中与操作系统无关的地方加载配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux下,我将配置放在〜/.programname"中.我应该将其放在Windows中的什么位置?在python中独立打开配置文件OS的推荐方式是什么?

under Linux I put my configs in "~/.programname". Where should I place it in windows? What would be the recommendated way of opening the config file OS independent in python?

谢谢!内森

推荐答案

尝试:

os.path.expanduser('~/.programname')

在linux上,它将返回:

On linux this will return:

>>> import os
>>> os.path.expanduser('~/.programname')
'/home/user/.programname'

在Windows上,它将返回:

On windows this will return:

>>> import os
>>> os.path.expanduser('~/.programname')
'C:\\Documents and Settings\\user/.programname'

这有点丑陋,所以您可能想要这样做:

Which is a little ugly, so you'll probably want to do this:

>>> import os
>>> os.path.join(os.path.expanduser('~'), '.programname')
'C:\\Documents and Settings\\user\\.programname'

值得一提的是,Windows计算机上的以下应用程序在我的 Documents and Settings \ user 文件夹中创建其配置文件夹:

For what it's worth, the following apps on my Windows machine create their config folders in my Documents and Settings\user folder:

  • Android
  • AgroUML
  • G子
  • IPython

哇,我只是注意到我在Linux示例中放了/user/.programname 而不是/home/user/.programname .固定.

EDIT 2: Oh wow, I just noticed I put /user/.programname instead of /home/user/.programname for the linux example. Fixed.

这篇关于从python中与操作系统无关的地方加载配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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