2

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.

ishikun
  • 428
  • 5
  • 21
  • I think your scipt will have to determine the os type , then move on to use particular os directory listing. stackoverflow question http://stackoverflow.com/questions/110362/how-can-i-find-the-current-os-in-python then move on to http://docs.python.org/2/library/filesys.html – metric-space Aug 12 '13 at 02:15
  • 1
    A common way to handle things like this is to have a separate configuration .ini file and to use something like the ConfigParser module to parse and use the values in it. Other options are to use a json format file or invent your own. – martineau Aug 12 '13 at 03:42

1 Answers1

1

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.

erlc
  • 652
  • 1
  • 6
  • 11