Getting the Appdata Directory with Python and PEP?
robotsondrugs at gmail.com
Mon Nov 25 22:10:47 CET 2013
On 2013.11.25 14:48, Eamonn Rea wrote:
> I've heard that there is a library that allows you to get the appdata directory for a given OS, but I'd like to do it myself, as a learning experience.
> Is there a built in way to get a users Appdata Directory? For example on OS X it's in '~/Library//Application Support/'. I can get the OS just fine (sys.platform and then storing it in my own way; example: darwin = OS X, just for my own readability), and I can get the home directory just fine (expanduser), but I have no idea how to get the appdata directory.
> One way I could think of doing it would be to just detect the os and join the string on like so (completely untested, but an idea);
> if os == 'OS X':
> appdata_dir = os.path.join(home_dir, '/Application Support/')
> But then that arises the problem of cross platform compatibility.
> So is here a good, cross platform solution to this problem?
I don't know about OS X, but on Windows Vista and later, there is os.environ['APPDATA']. I don't explicitly check for OS; instead, I see if
APPDATA exists as an environment variable:
user_data_dir = os.path.join(os.environ['APPDATA'], 'NoiseBot')
user_data_dir = os.path.join(os.environ['HOME'], '.NoiseBot')
I didn't even know that such a thing existed on OS X.
CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 10.0
More information about the Python-list