我可以将标准输出重定向到某种字符串缓冲区吗?

编程入门 行业动态 更新时间:2024-10-19 06:22:43
本文介绍了我可以将标准输出重定向到某种字符串缓冲区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用python的ftplib写了一个小的FTP客户端,但是包中的一些函数不返回字符串输出,而是打印到stdout.我想将 stdout 重定向到一个我可以从中读取输出的对象.

I'm using python's ftplib to write a small FTP client, but some of the functions in the package don't return string output, but print to stdout. I want to redirect stdout to an object which I'll be able to read the output from.

我知道 stdout 可以重定向到任何常规文件:

I know stdout can be redirected into any regular file with:

stdout = open("file", "a")

但我更喜欢不使用本地驱动器的方法.

But I prefer a method that doesn't uses the local drive.

我正在寻找类似于 Java 中的 BufferedReader 之类的东西,它可用于将缓冲区包装到流中.

I'm looking for something like the BufferedReader in Java that can be used to wrap a buffer into a stream.

推荐答案

from cStringIO import StringIO # Python3 use: from io import StringIO import sys old_stdout = sys.stdout sys.stdout = mystdout = StringIO() # blah blah lots of code ... sys.stdout = old_stdout # examine mystdout.getvalue()

更多推荐

我可以将标准输出重定向到某种字符串缓冲区吗?

本文发布于:2023-11-08 20:23:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓冲区   字符串   重定向   标准

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!