Can I redirect the stdout in python into some sort of string buffer?

前端 未结 9 1551
野趣味
野趣味 2020-11-22 06:23

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.

相关标签:
9条回答
  • 2020-11-22 06:54

    Here's another take on this. contextlib.redirect_stdout with io.StringIO() as documented is great, but it's still a bit verbose for every day use. Here's how to make it a one-liner by subclassing contextlib.redirect_stdout:

    import sys
    import io
    from contextlib import redirect_stdout
    
    class capture(redirect_stdout):
    
        def __init__(self):
            self.f = io.StringIO()
            self._new_target = self.f
            self._old_targets = []  # verbatim from parent class
    
        def __enter__(self):
            self._old_targets.append(getattr(sys, self._stream))  # verbatim from parent class
            setattr(sys, self._stream, self._new_target)  # verbatim from parent class
            return self  # instead of self._new_target in the parent class
    
        def __repr__(self):
            return self.f.getvalue()  
    

    Since __enter__ returns self, you have the context manager object available after the with block exits. Moreover, thanks to the __repr__ method, the string representation of the context manager object is, in fact, stdout. So now you have,

    with capture() as message:
        print('Hello World!')
    print(str(message)=='Hello World!\n')  # returns True
    
    0 讨论(0)
  • 2020-11-22 06:55

    Just to add to Ned's answer above: you can use this to redirect output to any object that implements a write(str) method.

    This can be used to good effect to "catch" stdout output in a GUI application.

    Here's a silly example in PyQt:

    import sys
    from PyQt4 import QtGui
    
    class OutputWindow(QtGui.QPlainTextEdit):
        def write(self, txt):
            self.appendPlainText(str(txt))
    
    app = QtGui.QApplication(sys.argv)
    out = OutputWindow()
    sys.stdout=out
    out.show()
    print "hello world !"
    
    0 讨论(0)
  • 2020-11-22 06:58
    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()
    
    0 讨论(0)
提交回复
热议问题