![]() |
|
[Tut] 7 Easy Steps to Redirect Your Standard Output to a Variable (Python) - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] 7 Easy Steps to Redirect Your Standard Output to a Variable (Python) (/thread-99425.html) |
[Tut] 7 Easy Steps to Redirect Your Standard Output to a Variable (Python) - xSicKxBot - 05-20-2022 7 Easy Steps to Redirect Your Standard Output to a Variable (Python) 5/5 – (1 vote) Question: How to redirect the standard output in Python and store it as a string in a variable?This article will guide you through seven easy steps to solve this problem. As an overview, here’s the code in eight lines that stores the standard output in a variable Code: my_result
Let’s go over those steps one by one—we’ll examine the full code for copy&paste at the end of this article, so read on! ![]() Step 1: Import libraries StringIO and sys Import the two libraries Code: StringIOCode: sysCode: from io import StringIO import sysBoth modules are part of the standard library, so there is no need to install them with pip! Step 2: Keep stdout in temporary variable We’ll overwrite the standard output to catch everything written to it. In order to reset your code to the normal state, we need to capture the original standard output stream by introducing a temporary variable. Code: tmp = sys.stdoutCreate a variable and assign a Code: StringIOCode: my_result = StringIO()Now, this object can store everything printed to the standard output. But we have to connect it first to the stdout! Step 4: Assign Standard Output Stream to StringIO object Assign the Code: StringIOCode: sys.stdoutCode: sys.stdout = my_resultFrom this point onwards, anything that is printed using the Code: print()Code: StringIOCode: my_resultThe following exemplifies the Code: print('hello world')Code: print('hello world') Note: No output appears on the screen anymore because the standard output is now redirected to the variable.Step 6: Clean up by redirecting stdout to Python shell Are you ready with capturing the output in the variable? Clean up by redirecting the standard output stream from the variable to the screen again. Code: sys.stdout = tmpAt this point, your string from the standard output is stored in the Code: StringIOCode: my_resultCode: StringIO.getvalue()Code: print(result.getvalue())Here’s the full code snippet for ease of copy&paste: Code: # Step 1
from io import StringIO
import sys # Step 2
tmp = sys.stdout # Step 3
my_result = StringIO() # Step 4
sys.stdout = my_result # Step 5
print('hello world') # Step 6
sys.stdout = tmp # Step 7
print('VARIABLE:', my_result.getvalue())
# hello worldYou can also check this out on our interactive Jupyter notebook so you don’t have to try in your own shell: ![]() Try it yourself: Click to run in Jupyter Notebook (Google Colab)References: Are you ready to up your Python skills? Join our free email academy — we’ve cheat sheets too!
|