在 Python 中复制流 [英] Copying a stream in Python

查看:24
本文介绍了在 Python 中复制流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中将流的内容传输到另一个流?

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

简单的解决方案是

output.write(input.read())

但如果输入文件大于可用内存(甚至无限大),则会失败;当部分副本也很有用时,它就不能很好地工作.基本上我正在寻找相当于 org.apache.commons.IOUtils.copy.

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.

推荐答案

shutil.copyfileshutil.copyfileobj 用于救援.请参阅 http://docs.python.org/library/shutil.html#module-shutil

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

这篇关于在 Python 中复制流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆