tags:

views:

63

answers:

2

I have created temporary named files, with the tempfile libraries NamedTemporaryFile method. I have written to them flushed the buffers, and I have not closed them (or else they might go away)

I am trying to use the subprocess module to call some shell commands using these generated files.

subprocess.call('cat %s' % f.name) always fails saying that the named temporary file does not exist.

os.path.exists(f.name) always returns true. I can run the cat command on the file directly from the shell.

Is there some reason the subprocess module will not work with temporary files?

Is there any way to make it work?

Thanks in advance.

+1  A: 

Are you using shell=True option for subprocess?

Noah
I was being stupid, setting shell to true fixed the errors but there was another file not found error, where the file actually didnt exist.
Matt
+1  A: 

Why don't you make your NamedTemporaryFiles with the optional parameter delete=False? That way you can safely close them knowing they won't disappear, use them normally afterwards, and explicitly unlink them when you're done. This way everything will work cross-platform, too.

Alex Martelli