python怎么存数据_如何存储Python应用程序数据

1586010002-jmsa.png

I'm creating a simple Python CLI tool, which lets the user add and delete tasks (the classic Todo app). This is just for my own use, but I want to get into the best practices of creating such applications. The data will be stored in a simple text file.

Main question: Where should I store the data file? After doing some reading, I'm inclined to create a new folder in /var/lib and keep the data.txt file in that directory. Are there any drawbacks to that option?

Follow up question: Since, by default, only root has access to /var, do I need to change the permissions for the whole /var directory in order to read and write to the data file?

解决方案

User data should be store in the user's home directory. You could use

/Users/joe/.myclitool/data.txt

under Mac OS X and

/home/joe/.myclitool/data.txt

under GNU/Linux.

In Python this can be done with

import os

import os.path

p = os.path.join(os.getenv("HOME"), ".myclitool", "data.txt")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值