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?
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
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With