问题
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