Copying data between file-like objects

Fredrik Lundh fredrik at pythonware.com
Tue Feb 15 11:52:49 EST 2005


Thomas Lotze wrote:

> f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a
> string containing all the contents of f2 will be created and thrown away.

if f2 isn't too large, reading lots of data in one operation is often the most
efficient way (trust me, the memory system is a lot faster than your disk)

if you don't know how large f2 can be, use shutil.copyfileobj:

    >>> help(shutil.copyfileobj)
    Help on function copyfileobj in module shutil:

    copyfileobj(fsrc, fdst, length=16384)
        copy data from file-like object fsrc to file-like object fdst

> In the case of two StringIO objects, this means there's a point when the
> contents is held in memory three times.

to copy stringio objects, you can use f1 = StringIO(f2.getvalue()).  why you
would want/need to do this is more than I can figure out, though...

</F> 






More information about the Python-list mailing list