Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary directory persist across program runs

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?

like image 219
umpirsky Avatar asked Jan 09 '12 13:01

umpirsky


1 Answers

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.)

like image 55
Fred Foo Avatar answered Sep 21 '22 21:09

Fred Foo