How to store Python application data

血红的双手。 提交于 2019-12-11 05:06:39

问题


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?


回答1:


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")


来源:https://stackoverflow.com/questions/24608665/how-to-store-python-application-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!