Temporary directory persist across program runs
Question
I need a temporary directory, but I want full control over its creation and deletion.
I will use this directory to place git repositories which I want to monitor for new commits, so I need to store them somewhere permanently.
Therefore I want to avoid /tmp
dir, since it can be cleared by user(?). What is the best practice for this?
Solution
http://pypi.python.org/pypi/appdirs is a Python module that offers a cross-platform user_cache_dir
function.
OTHER TIPS
tempfile.mkdtemp
will create a temp dir for you and return its name. It will create it in /tmp
by default (on Unix-like systems), but "in the most secure manner possible" and with read/write/list permissions only for the caller's user id.
>>> d = tempfile.mktemp()
>>> with open(os.path.join(d, "secret")) as output:
... output.write("Ha, you can't read this!")
(Btw., on a Unix/Linux system with default settings, users can't just edit or remove each others' files from /tmp
.)
I'd say that the best practice is to use tempfile.mkdtemp
.
If you don't wan to use /tmp
then, you can take advantage of the prefix
parameter:
import tempfile
tempfile.mkdtemp(prefix=<your_preferred_directory>)
Edit: Regarding what's the most appropriate directory to sotre your application configuration, cache data, etc. If you're using linux, please have a look at the XDG Base Directory Specification.
If it's really temporary, follow larmans' advice and use mkdtemp
.
If it's some sort of semi-permanent cache that must survive reboots, then you should use the local application directory, as defined by your OS (%APPDATA%, ~/.local/ etc); some toolkits (e.g. Qt) provide functions to look that folder up in a cross-platform manner.
Edit: from Wikipedia:
- HOME (Unix-like) and USERPROFILE (Microsoft Windows) - indicate where a user's home directory is located in the file system.
- HOME/{.AppName} (Unix-like) and APPDATA{DeveloperName\AppName} (Microsoft Windows) - for storing application settings. Many open source programs incorrectly use USERPROFILE for application settings in Windows - USERPROFILE should only be used in dialogs that allow user to choose between paths like Documents/Pictures/Downloads/Music, for programmatic purposes use APPDATA (roaming), LOCALAPPDATA or PROGRAMDATA (shared between users)
So you should look up os.environ['APPDATA']
or os.environ['HOME']
, depending on platform (see sys.platform
) and then append your app name, and then you can store there anything you want.
mydir = os.path.join( ".myAppName", "cache")
homeVar = 'HOME' # default for all *nix variants
if sys.platform == 'win32':
homeVar = 'APPDATA'
mydir = os.path.join( os.environ[homeVar], mydir)
Usually programs use a ~/.progname
directory to store data that should be persistent but should stay "out of the way" of the user.
Just a though: You may want to look into git commit hooks. That way, instead of monitoring a tmp directory for new commits (sounds strange: who would commit into a tmp directory with limited permissions?) the repo informs you about commits, or, more specifically, automatically runs a script whenever a commit occurs..