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

前端 未结 9 1552
野趣味
野趣味 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: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()
    

提交回复
热议问题