Copying a stream in Python

13,148

shutil.copyfile and shutil.copyfileobj for the rescue. See http://docs.python.org/library/shutil.html#module-shutil

Share:
13,148
phihag
Author by

phihag

Hi, I'm Philipp Hagemeister. You can contact me at [email protected] or a number of other communication channels.

Updated on June 14, 2022

Comments

  • phihag
    phihag almost 2 years

    How do I transfer the contents of a stream to another in Python?

    The trivial solution would be

    output.write(input.read())
    

    but that fails if the input file is larger than the available memory (or even infinitely large); and it doesn't work well when a partial copy is useful as well. Basically I'm looking for the equivalent of org.apache.commons.IOUtils.copy.