Copying data between file-like objects

Thomas Lotze thomas at thomas-lotze.de
Tue Feb 15 12:25:14 EST 2005


Fredrik Lundh wrote:

> 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)

Sure.

> 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

This sounds like what I was looking for. Thanks for the pointer.
However, the following doesn't seem like anything is being copied:

>>> from StringIO import StringIO
>>> from shutil import copyfileobj
>>> s = StringIO()
>>> s2 = StringIO()
>>> s.write('asdf')
>>> copyfileobj(s, s2)
>>> s2.getvalue()
''

> to copy stringio objects, you can use f1 = StringIO(f2.getvalue()).

But this should have the same problem as using read(): a string will be
created on the way which contains all the content.

> why you
> would want/need to do this is more than I can figure out, though...

Because I want to manipulate a copy of the data and be able to compare it
to the original afterwards.

Another thing I'd like to do is copy parts of a StringIO object's content
to another object. This doesn't seem possible with any shutil method. Any
idea on that?

What one can really wonder, I admit, is why the difference between holding
data two or three times in memory matters that much, especially if the
latter is only for a short time. But as I'm going to use the code that
handles the long string as a core component to some application, I'd like
to make it behave as well as possible.

-- 
Thomas





More information about the Python-list mailing list