views:

1036

answers:

6

I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want.

Currently, I'm doing the following to create the directory:

randName = "temp" + str(random.randint(1000, 9999))
os.makedirs(randName)

And when I want to delete the directory, I just look for a directory with "temp" in it.
This seems like a dirty hack, but I'm not sure of a better way at the moment.

Incidentally, the reason that I need the folder around is that I start a process that uses the folder with the following:

subprocess.Popen([command], shell=True).pid

and then quit my script to let the other process finish the work.

+3  A: 

A temporary file is something that lasts for a single program run.

What you need is not, therefore, a temporary file.

Also, beware of multiple users on a single machine - just deleting anything with the 'temp' pattern could be anti-social, doubly so if the directory is not located securely out of the way.

Also, remember that on some machines, the /tmp file system is rebuilt when the machine reboots.

Jonathan Leffler
yea, I think I need a temporary-ish file. It really does only exist for one run, but the run continues after my script ends.
John Mulder
+10  A: 

Creating the folder with a 4-digit random number is insecure, and you also need to worry about collisions with other instances of your program.

A much better way is to create the folder using tempfile.mkdtemp, which does exactly what you want (i.e. the folder is not deleted when your script exits). You would then pass the folder name to the second Popen'ed script as an argument, and it would be responsible for deleting it.

dF
+1  A: 

tempfile is just fine, but to be on a safe side you'd need to safe a directory name somewhere until the next run, for example pickle it. then read it in the next run and delete directory. and you are not required to have /tmp for the root, tempfile.mkdtemp has an optional dir parameter for that. by and large, though, it won't be different from what you're doing at the moment.

SilentGhost
+3  A: 

"I need to create a folder that I use only once, but need to have it exist until the next run."

"Incidentally, the reason that I need the folder around is that I start a process ..."

Not incidental, at all. Crucial.

It appears you have the following design pattern.

mkdir someDirectory
proc1 -o someDirectory # Write to the directory
proc2 -i someDirectory # Read from the directory
if [ %? == 0 ]
then
    rm someDirectory
fi

Is that the kind of thing you'd write at the shell level?

If so, consider breaking your Python application into to several parts.

  • The parts that do the real work ("proc1" and "proc2")

  • A Shell which manages the resources and processes; essentially a Python replacement for a bash script.

S.Lott
+4  A: 

What you've suggested is dangerous. You may have race conditions if anyone else is trying to create those directories -- including other instances of your application. Also, deleting anything containing "temp" may result in deleting more than you intended. As others have mentioned, tempfile.mkdtemp is probably the safest way to go. Here is an example of what you've described, including launching a subprocess to use the new directory.

import tempfile
import shutil
import subprocess

d = tempfile.mkdtemp(prefix='tmp')
try:
    subprocess.check_call(['/bin/echo', 'Directory:', d])
finally:
    shutil.rmtree(d)
Ryan Bright
A: 

The best way of creating the temporary file name is either using tempName.TemporaryFile(mode='w+b', suffix='.tmp', prifix='someRandomNumber' dir=None) or u can use mktemp() function.

The mktemp() function will not actually create any file, but will provide a unique filename (actually does not contain PID).