tags:

views:

274

answers:

6

My application caches some data on disk. Because the cache may be large, it should not be stored on a network drive. It should persist between invocations of the application. I have a mechanism for the user to choose a location, but would like the default to be sensible and "the right thing" for the platform.

What is the appropriate location for such a cache? Is there an API for determining the appropriate location? How do I call it from Python?

A: 

Perhaps the tempfile module provides what you need. It uses the Windows Temp directory (which probably is not on a network drive) but you can specify a directory if you want to. Also for security reasons this module should be the right tool - if you use tempfile.mkstemp() the file is readable and writable only by the creating user ID.

Oh. I see you have just edited your question and that you need file persistence between invocations of the app. Then tempfile is not that ideal (even though you could choose not to delete your cache between invocations).

Tim Pietzcker
A: 

Does the app have any preferences, settings or options that the user can specify? If so, add an option where the user can specify the location of the data, with a default of the current Windows temp directory.

There's always a chance they may not have enough space on the drive with the temp directory, and would need to use a different drive/directory.

Patrick Cuff
+3  A: 

There are a number a places you can put your application files in Windows. This page shows a list (this enum is .Net specific but most of the special folders are standard on Windows in general). Basically you'll need to decide if you need a cache per user, only for the local machine, per application or shared, etc.

I don't have much experience with python so I cannot specifically help with how to get these paths, but I'm sure someone more knowledgeable here can.

akmad
+2  A: 

The standard location for Windows application to store their (permanent) application data is referenced by the %APPDATA% (current user) or %ALLUSERSPROFILE% (all users) environment variables. You can access them using, e.g. (only rudimentary and not very elegant error checking!):

import os
app_path = os.getenv("APPDATA") + "\\MyApplicationData"
try:
    os.mkdir(app_path)
except WindowsError:
    # already exists

Now you have your own directory for your app.

Tim Pietzcker
+2  A: 

Have a look here: http://en.wikipedia.org/wiki/Environment_variable#User_management_variables. Anything that's under the users directory is good. If its for all users, then it should be: %ALLUSERSPROFILE%. If its for a specific user, make sure the permissions are right.

Check out MSDN for more info about other Windows Versions. Environment variables can vary from systems to systems.

Loki
what I'm reading is that, for normal users, the %ALLUSERSPROFILE% folder is read-only. Not sure but that's what people are saying...
paul
+1  A: 

The wx.StandardPaths module contains methods that return various standard locations in the file system and transparently tries to do "the right thing" under Unix, Mac OS X, and Windows.

Vebjorn Ljosa