In Python 3.6 I am trying to but a database called test.db
in the current user's home directory. Currently, I am getting that directory with home = os.path.expanduser("~")
(after importing os
). My problem is that when I run s = shelve.open(home + "/test")
it creates the file in /path/to/current/python/file/Users/USERNAME/test.db
. Is there a way to shelve a database by an absolute path, e.g. /Users/USERNAME/test.db
? Also, can I make it cross platform; Windows would require shelve.open(home + "\test")
and Mac/Linux would require shelve.open(home + "/test")
? Thanks.
To make it cross platform, use os.path.join()
to join paths
import os, shelve
path = os.path.join(os.path.expanduser("~"), 'test')
shelve.open(path)