Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store desktop application data in a cross platform way for python?

I have a python desktop application that needs to store user data. On Windows, this is usually in %USERPROFILE%\Application Data\AppName\, on OSX it's usually ~/Library/Application Support/AppName/, and on other *nixes it's usually ~/.appname/.

There exists a function in the standard library, os.path.expanduser that will get me a user's home directory, but I know that on Windows, at least, "Application Data" is localized into the user's language. That might be true for OSX as well.

What is the correct way to get this location?

UPDATE: Some further research indicates that the correct way to get this on OSX is by using the function NSSearchPathDirectory, but that's Cocoa, so it means calling the PyObjC bridge...

like image 893
Douglas Mayle Avatar asked Jul 05 '09 19:07

Douglas Mayle


People also ask

Can Python be used for desktop applications?

Can you create a desktop application using Python? Yes, you can. In this article, we will learn how to make a desktop application using the Tkinter library of Python. There are several libraries in Python, but Tkinter is one of the easiest ones among them.

Are Python applications cross-platform?

Python is a cross-platform, interpreted, object-oriented programming language that is perfectly suited for Rapid Application Development, scripting, and connecting existing components together. Python uses a simple, easy to learn syntax, which focuses on readability to reduce the overall cost of program maintenance.

Is KIVY good for desktop apps?

Kivy is a platform independent as it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it Does not mean that it can not be used on Desktops applications.


1 Answers

Well, I hate to have been the one to answer my own question, but no one else seems to know. I'm leaving the answer for posterity.

APPNAME = "MyApp" import sys from os import path, environ if sys.platform == 'darwin':     from AppKit import NSSearchPathForDirectoriesInDomains     # http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains     # NSApplicationSupportDirectory = 14     # NSUserDomainMask = 1     # True for expanding the tilde into a fully qualified path     appdata = path.join(NSSearchPathForDirectoriesInDomains(14, 1, True)[0], APPNAME) elif sys.platform == 'win32':     appdata = path.join(environ['APPDATA'], APPNAME) else:     appdata = path.expanduser(path.join("~", "." + APPNAME)) 
like image 195
Douglas Mayle Avatar answered Oct 07 '22 15:10

Douglas Mayle