【发布时间】:2017-07-17 05:39:31
【问题描述】:
在 Python 3.6 中,我试图在当前用户的主目录中创建一个名为 test.db 的数据库。目前,我正在使用home = os.path.expanduser("~") 获取该目录(在导入os 之后)。我的问题是,当我运行s = shelve.open(home + "/test") 时,它会在/path/to/current/python/file/Users/USERNAME/test.db 中创建文件。有没有办法通过绝对路径搁置数据库,例如/Users/USERNAME/test.db?另外,我可以让它跨平台吗? Windows 需要shelve.open(home + "\test") 而Mac/Linux 需要shelve.open(home + "/test")?谢谢。
【问题讨论】:
-
为什么投反对票?
-
我没有投反对票,但我认为这里应该有一个演示脚本。您的代码应该可以工作。我不知道'/path/to/python/file`是什么......它是你的python脚本的路径......你不在那个目录中吗?但是一个带有少量印刷品的简短脚本会使这一点更加清晰。
标签: python directory absolute-path shelve python-3.6