create account

每天进步一点点:配置文件 & appdirs by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$53.41
每天进步一点点:配置文件 & appdirs
对于很多应用程序来讲,为了方便用户使用,都需要保存一些信息,比如配置信息、用户数据等等。这一般有几种方式,比如说配置文件、注册表、数据库等,本质上Windows中的注册表也是一种数据库。


![image.png](https://images.hive.blog/DQmZK97WoSNVCupacEEB5zZh3kneM6fAocJf8nMHnEyD1Rm/image.png)
(图源 :[pixabay](https://pixabay.com/))

# 注册表

这几种方式我最不喜欢的就是注册表了,尽管注册表操作起来还是很简单的(`RegOpenKeyEx`、`RegQueryValueEx`、`RegSetValueEx`等),但是我总觉得,这样做应用程序就不是那么绿色环保了。

那些所谓的安全卫士等软件,很多也是靠清理垃圾注册表项才发家的,所以我觉得Windows应用越少写注册表越好,所以自己的程序也尽量避免。

# 配置文件

Windows下提供了很方便的配置文件操作API,主要有两个函数:`GetPrivateProfileString`以及`WritePrivateProfileString`。

尽管文档中说了相关API只是为了兼容和保留,推荐使用注册表:
>***Note***  This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.

但是Win10中,相关函数依然工作正常,我又不喜欢用注册表,那当然还是用它喽,比如我的字模程序,用着就好好的:

# Python 中的配置文件

Python中简单一些配置文件,直接用普通的文件读写就搞定了(或者直接用py文件导入)。

略复杂配置的可以用[configparser](https://docs.python.org/3/library/configparser.html)来实现,用起来还是超级简单的。

这里不做过多介绍了,大家感兴趣的话看文末链接文档就好。

# 用户/应用目录等

配置文件一般可以保存在和执行程序相同的目录,但是假设有N个在不同目录中的程序可能用到相同的内容,放到执行程序所在的目录就不是那么明智了。

Windows中可以选将一些设置和数据放到用户目录,比如说`%USERPROFILE%` `%APPDATA% ` 等,这些目录可以通过环境变量获取,比如Windows如下命令:
>`echo %APPDATA%`

就会返回:
>![image.png](https://images.hive.blog/DQmbXHTLWpxVrDqjLBELJqe9TWphqbQJ9tFGs3YKKAb7pde/image.png)

在Windows程序中,则可以通过`GetEnvironmentVariable`函数获取。


# appdirs 

说了半天,这个才是重点,就是Linux环境下,Python怎么获取用户数据目录或者应用目录。

这就要用到appdirs这个模块了,其实这个模块在Windows下也能用,不过就不去探究了。

在Linux下,使用如下方式,就可以获取用户/应用对应的数据目录了:
>`from appdirs import user_data_dir`
`appname="cutehive"`
`appauthor = "oflyhigh"`
`config_dir = user_data_dir(appname, appauthor)`

上述代码得到的目录路径为:
>`.local/share/cutehive`

这样,我们就可以在对应的目录中愉快地写入配置文件、数据、数据库等内容了,而不必自己关心和维护路径信息了。

这样做还有一个好处就是,支持跨平台操作(Windows、MacOS等),然而对我而言,倒是暂时不需要考虑呢。

# 相关链接
* https://docs.microsoft.com/en-us/windows/win32/api/winreg/
* https://docs.microsoft.com/en-us/windows/win32/api/winbase/
* https://docs.python.org/3/library/configparser.html
* https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable
* https://github.com/ActiveState/appdirs
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 126 others
properties (23)
authoroflyhigh
permlinkand-appdirs
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","cutehive","cn-programming","study","appdirs","python"],"image":["https://images.hive.blog/DQmZK97WoSNVCupacEEB5zZh3kneM6fAocJf8nMHnEyD1Rm/image.png","https://images.hive.blog/DQmbXHTLWpxVrDqjLBELJqe9TWphqbQJ9tFGs3YKKAb7pde/image.png"],"links":["https://pixabay.com/","https://docs.python.org/3/library/configparser.html","https://docs.microsoft.com/en-us/windows/win32/api/winreg/","https://docs.microsoft.com/en-us/windows/win32/api/winbase/","https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable","https://github.com/ActiveState/appdirs"],"app":"hiveblog/0.1","format":"markdown"}
created2020-06-02 04:52:09
last_update2020-06-02 04:52:09
depth0
children3
last_payout2020-06-09 04:52:09
cashout_time1969-12-31 23:59:59
total_payout_value28.619 HBD
curator_payout_value24.786 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,047
author_reputation6,444,691,484,718,559
root_title"每天进步一点点:配置文件 & appdirs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,724,018
net_rshares109,184,751,118,157
author_curate_reward""
vote details (190)
@anri.rixardinh ·
Trade on all cryptocurrency exchanges from one interface! -  https://kintum.io/
👍  
properties (23)
authoranri.rixardinh
permlinkqber37
categoryhive-105017
json_metadata{"links":["https://kintum.io/"],"app":"hiveblog/0.1"}
created2020-06-04 15:30:45
last_update2020-06-04 15:30:45
depth1
children0
last_payout2020-06-11 15:30:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length79
author_reputation-1,331,112,739,183
root_title"每天进步一点点:配置文件 & appdirs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,771,295
net_rshares2,487,216,310
author_curate_reward""
vote details (1)
@davidke20 ·
來支持囖🤫
properties (22)
authordavidke20
permlinkre-oflyhigh-202062t132434746z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","cutehive","cn-programming","study","appdirs","python"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"}
created2020-06-02 05:24:36
last_update2020-06-02 05:24:36
depth1
children0
last_payout2020-06-09 05:24:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5
author_reputation955,625,340,204,055
root_title"每天进步一点点:配置文件 & appdirs"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,724,327
net_rshares0
@littleksroad ·
來支持一下!
properties (22)
authorlittleksroad
permlinkqbau32
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2020-06-02 12:45:03
last_update2020-06-02 12:45:03
depth1
children0
last_payout2020-06-09 12:45:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation75,907,335,963,103
root_title"每天进步一点点:配置文件 & appdirs"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,729,111
net_rshares0