本文介绍了我可以将标准输出重定向到某种字符串缓冲区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用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()更多推荐
我可以将标准输出重定向到某种字符串缓冲区吗?
发布评论