简体   繁体   中英

Application's Datapath - os.environ

I am developing a Python Desktop application and I have been able to get it to work just fine, I am just having trouble getting classes to locate the database.

This application was build on the Windows platform and used the following code to create the database.

appDataPath = os.environ["APPDATA"] + "\\FolderName\\"

However, in mac, there is no APPDATA and I have been using HOME instead, but it creates the files on build as FolderNameapplication.db which is a real hassle.

The database is now residing on the Desktop (HOME) after removing the `"\\FolderName\\" extension and I need the appDataPath variable to point at the database. How can I achieve this?

Thanks

Use os.path.join() to construct paths. OS X (and Linux) don't use backslashes for paths like Windows does so adding those strings with hard-coded path separators will cause you problems. (Although forward slashes can work on Windows...)

Example: os.path.join(os.environ['HOME'], 'somedir', 'mydb.db')

The usual OS X alternative to appdata is ~/Library/Application Support/appname/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM