Reputation: 10024
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?
Upvotes: 9
Views: 4684
Reputation: 25197
https://pypi.python.org/pypi/platformdirs is a Python package that offers a cross-platform user_cache_dir
function.
Upvotes: 1
Reputation: 1823
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:
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)
Upvotes: 2
Reputation: 40374
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.
Upvotes: 8
Reputation: 3328
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..
Upvotes: 0
Reputation: 229563
Usually programs use a ~/.progname
directory to store data that should be persistent but should stay "out of the way" of the user.
Upvotes: 0
Reputation: 363477
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
.)
Upvotes: 8