Question

I have a python program that I've written that requires a specific database that is located in a folder on my computer directory.

Currently my program initializes the database in the same location as where the python script is found, and I thought this is the best way to do it so that the database stays 'static' relative to the script.

My question is, is there a different way of doing this? Perhaps some kind of best practice? Also, what exactly is this concept called?

This is my first time to write a program for other people to use and I'm a bit nervous that it might break somewhere.

Was it helpful?

Solution

I would use the user's home folder and use the os.path module. Let's say you have a program named myapp and a database called db.sqlite.

import os.path
path = os.path.expanduser('~/.myapp/db.sqlite')

path would then be expanded to /home/user/.myapp/db.sqlite on a UNIX-based system (and similar for Mac) and C:\Users\user\Application Data\Roaming.myapp\db.sqlite (or something similar, not on a windows machine) on a Windows machine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top