Redirecting Standard Output using StringIO and Sys module - Python
Table of contents
No headings in the article.
Sometimes, when writing a program there could be a reason not to like writing into a file directly. In programming, one of the practices is to write data into a file because after a program runs finish all the data is lost, to prevent this, programmers write into the file when there is no database or the data is too short or not necessarily for the database.
You can prevent additional code in writing into a file by using the StringIO module available in python, which is in conjunction with the sys module to perform what I called the complex method of writing into a file.
The sys module has two methods stdout and __stdout__(it has more methods}:
The formal can easily be redirected to StringIO while the latter holds the standard output for reinitialize when already using the formal and later initialized. what we are saying here is that StringIO captures the print function and writes Its argument into a StringIO object -a file-like object.
The stringIO is present in the io module - input and output module - it should be imported when needed
The output:
This is the place we are drawing the veil. Thank you for reading!