[Python-Dev] Rationale for NamedTemporaryFile revisited [SEC=UNCLASSIFIED]
Guido van Rossum
guido at python.org
Fri Jan 18 05:45:27 CET 2008
Don't close it until you're done with it. Isn't that obvious?
On Jan 17, 2008 8:30 PM, <Ole.Nielsen at ga.gov.au> wrote:
> I found this posting, and those following it, as I too am baffled that
> NamedTemporaryFile doesn't let you read the generated file - even within the
> same script.
> For unit testing it is very commonplace to generate a test file on the fly
> and then use it as input the function being tested. NamedTemporaryFile would
> be the perfect candidate for this except that the file seems to disappear
> immediately after it has been closed. This is contrary to Dustin's comment
> which states that the lifetime extends until the object is garbage collected.
> The following script illustrates this isn't the case:
>
> from tempfile import NamedTemporaryFile
> fid = NamedTemporaryFile(mode='w',
> suffix='.tmp',
> dir='.')
>
> fid.write('My temp file')
> fid.close()
>
> # Read it (fid hasn't been garbage collected yet)
> print fid.name
>
> # But the file itself has gone
> fid2 = open(fid.name)
> print fid2.readlines()
>
>
> Can someone please help point me to the best way of creating temporary files
> in Python that can be read in immediately after their creation.
>
> Thanks
> Ole Nielsen, Geoscience Australia
>
>
>
> On Sun, Mar 18, 2007 at 11:49:55AM +1200, Greg Ewing wrote:
> > I've just discovered the hard way that NamedTemporaryFile
> > automatically deletes the file when you close it. That
> > doesn't seem very useful to me, since surely the reason
> > you're using NamedTemporaryFile instead of TemporaryFile
> > is that you want to do something else with it afterwards?
> > What's the rationale for this behaviour?
>
> For both TemporaryFile and NamedTemporaryFile, the rationale is that
> "temporary" extends until the object is garbage collected.
> TemporaryFile is deleted immediately (to prevent other applications from
> modifying your temporary file). NamedTemporaryFile is inteded for use
> when you need access to the file by filename during the lifetime of the
> file. In either case, when the object goes, the file should too.
>
> Dustin
>
>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list