Capturing Program Output: Beyond Naïve Solutions
In Python scripting, capturing program output for further processing is a common need. While naïve solutions may seem straightforward, they often fall short. Consider the following script that writes to stdout:
# writer.py import sys def write(): sys.stdout.write("foobar")
Attempting to capture the output using the following code fails:
# mymodule.py from writer import write out = write() print(out.upper())
To effectively capture the output, a more robust solution is required. One approach involves modifying the system's stdout stream:
import sys from cStringIO import StringIO # Redirect stdout to a StringIO object backup = sys.stdout sys.stdout = StringIO() # Perform the write operation write() # Retrieve and restore stdout out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup # Process the captured output print(out.upper())
Context Manager for Python 3.4 :
For Python 3.4 and later, a simpler and more concise solution is available using the contextlib.redirect_stdout context manager:
from contextlib import redirect_stdout import io f = io.StringIO() # Redirect stdout to f using the context manager with redirect_stdout(f): help(pow) # Retrieve captured output from f s = f.getvalue()
This elegant approach simplifies the output capturing process, making it easier to handle in your Python scripts.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3