Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] 7 Easy Steps to Redirect Your Standard Output to a Variable (Python)

#1
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 my_result:

  1. from io import StringIO
  2. import sys
  3. tmp = sys.stdout
  4. my_result = StringIO()
  5. sys.stdout = my_result
  6. print('hello world') # output stored in my_result
  7. sys.stdout = tmp
  8. print(result.getvalue())

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 StringIO and sys to access the standard output and store the string input-output stream.

from io import StringIO import sys

Both 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.

tmp = sys.stdout

Step 3: Capture standard output using a StringIO object


Create a variable and assign a StringIO object to the variable to capture the standard output stream.

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 StringIO object created in the previous step to the standard output that is captured with sys.stdout.

sys.stdout = my_result

Step 5: Print to the standard output


From this point onwards, anything that is printed using the print() statement by any function you call in your Python script is written in the StringIO object referred to by variable my_result.

The following exemplifies the print('hello world') statement but you can do anything here:

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.

sys.stdout = tmp

Step 7: Get and print the string from stdout


At this point, your string from the standard output is stored in the StringIO object in the my_result variable. You can access it using the StringIO.getvalue() method.

print(result.getvalue())

Full Code


Here’s the full code snippet for ease of copy&paste:

# 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 world

You 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! ?



https://www.sickgaming.net/blog/2022/05/...le-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python IndexError: Tuple Index Out of Range [Easy Fix] xSicKxBot 0 1,949 08-22-2023, 09:07 AM
Last Post: xSicKxBot
  [Tut] Python Snake Made Easy xSicKxBot 0 1,215 04-25-2023, 05:36 PM
Last Post: xSicKxBot
  [Tut] Python ? Put Legend Outside Plot ? – Easy Guide xSicKxBot 0 1,463 04-22-2023, 11:08 PM
Last Post: xSicKxBot
  [Tut] Easy Way to Update a Python Package with Pip Upgrade xSicKxBot 0 1,381 03-19-2023, 12:16 PM
Last Post: xSicKxBot
  [Tut] I Created My First DALL·E Image in Python OpenAI Using Four Easy Steps xSicKxBot 0 1,276 03-10-2023, 03:46 PM
Last Post: xSicKxBot
  [Tut] How to Install Pip? 5 Easy Steps xSicKxBot 0 1,203 02-28-2023, 06:55 AM
Last Post: xSicKxBot
  [Tut] EzpzShell: An Easy-Peasy Python Script That Simplifies Revshell Creation xSicKxBot 0 1,343 02-11-2023, 10:30 AM
Last Post: xSicKxBot
  [Tut] Two Easy Ways to Encrypt and Decrypt Python Strings xSicKxBot 0 1,309 02-02-2023, 12:29 PM
Last Post: xSicKxBot
  [Tut] I Used These 3 Easy Steps to Create a Bitcoin Wallet in Python (Public/Private) xSicKxBot 0 1,221 01-29-2023, 02:51 AM
Last Post: xSicKxBot
  [Tut] Spectacular Titles: An Easy Python Project Generating Catchy Titles xSicKxBot 0 1,192 01-01-2023, 12:21 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016