Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess module: using the call method with tempfile objects

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.

like image 490
Matt Avatar asked Feb 11 '26 21:02

Matt


1 Answers

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.

like image 141
Alex Martelli Avatar answered Feb 15 '26 22:02

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!