tempfile.NamedTemporaryFile use case?
Roy Smith
roy at panix.com
Fri Dec 31 17:37:16 EST 2010
What is the use case for tempfile.NamedTemporaryFile? As far as I can
tell, the only way it differs from TemporaryFile is that it is
guaranteed to have a name in the file system. BUT, it's not guaranteed
that you can open the file a second time via that name.
So, what's the point? In what situations would NamedTemporaryFile do
what you want but TemporaryFile not?
I'm writing a unit test where I want to verify operation of my code on a
path which can't be opened (i.e. that it raises IOError).
NamedTemporaryFile almost gives me what I want. It creates a file,
tells me what the path is (so I can os.chmod() it to mode 0), and cleans
it up when I'm done (so I don't have to write my own context manager or
whatever). But, it's not guaranteed that I can open the path, so the
whole test is moot.
I can work around that (plain old mktemp() or mkstemp() and have my
tearDown() method do the cleanup), but the more I look at this, the more
I'm scratching my head why NamedTemporaryFile exists.
More information about the Python-list
mailing list