tempfile.NamedTemporaryFile wont work

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sun Nov 19 08:49:29 EST 2006


On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:

> On suse 9.3, tempfile.NamedTemporaryFile() doesnt work as expected.
[snip]

> Symptom: the file does not always exist, after the call to
> NamedTemporaryFile(). Or at least its not seen by the strings command,
> or by os.path.exists.
> 
> I guess the bug is pretty much os dependent, or even filesystem
> dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
> python interface. Or did I miss something?
> Shame, I didnt even try to check for a python bug tracker.

I can verify this problem occurs on Fedora Core 5 too:

import os
import sys
import tempfile
import subprocess
def test(n):
	chunk = ': +++ abcd +++'
	for i in xrange(n):
		tf = tempfile.NamedTemporaryFile()
		tfName = tf.name
		tf.seek(0)
		tf.write(str(i) + chunk)
                tf.flush()
		if not os.path.exists(tfName):
			print 'pre-check: %s not there' % tfName
		subprocess.Popen(['strings', tfName])
		if not os.path.exists(tfName):
			print 'post-check: %s not there' % tfName


And here is a typical run, with the boring bits removed for ease of
reading:

>>> test(30)
0: +++ abcd +++
1: +++ abcd +++
    [ more of the same ]
14: +++ abcd +++
strings: '/tmp/tmpOALbx9': No such file
16: +++ abcd +++
17: +++ abcd +++
18: +++ abcd +++
    [ more of the same ]
27: +++ abcd +++
strings: /tmp/tmpdc52Nz: No such file or directory
29: +++ abcd +++


Curiouser and curiouser... not only does os.path.exist always report the
temp file as existing (at least in my tests), even when strings can't find
it, but strings returns different error messages.

Is it possible this is a bug in strings?


-- 
Steven.




More information about the Python-list mailing list