Temporary Files

Tony Meyer ta-meyer at ihug.co.nz
Wed Apr 30 01:57:11 EDT 2003


[me]
> Is there perhaps a different method of copying?  One that 
> doesn't take filenames, but file objects?

Ack.  Yes, there is shutil.copyfileobj().

*sigh*  Rephrased in light of this rather belated discovery; is this
what I should be doing?

>>> import shutil
>>> import tempfile
>>> out = tempfile.TemporaryFile("w")
>>> out.write(data)
>>> out.flush()
>>> # shutil.copyfile(out.name, permanent_name)  # not this!
>>> permanent_file = file(permanent_name)
>>> shutil.copyfileobj(out, permanent_file)
>>> out.close()
>>> permanent_file.close()

TIA.

=Tony Meyer






More information about the Python-list mailing list