Welcome to the third and the last tutorial of the Python Print series. By the end of this tutorial, you will be the master of Python Print function. So, in this tutorial, I will show you how to automatically redirect the output stream in Python Programming.
The only problem with manual redirection of the output stream is that we have to mention the “file parameter” every time we want to save the output into the file. Which is fine, but only when we have to save the output of one or maybe two or a specific print statement. This approach is not ideal when we want to save the output returned by all the Python Print statements of the program.
Mentioning file parameter with all the python print statements in a program is not only tiresome but also prone to errors.
Manual Redirection Of Python Print
In the manual redirection of the output stream, we use the ‘file’ parameter of the Python Print statement to tell the interpreter to save the output returned by a function into a file. I have explained the concept of manual redirection here. Go ahead and check that out!
Automatic Redirection Of Python Print
In automatic redirection, we modify the standard output stream and set it to save the output into a file rather than showing it on the screen. The benefit of doing this modification is that you don’t have to rely on the ‘file’ parameter for permanently saving the output onto your hard-disk.
However, the only problem with the automatic redirection of the output stream is that resetting it to its default state requires a little bit of extra effort.
What you will learn today—
In this tutorial, I will show you, how to –
- Automatically redirect the output stream and
- Reset it back to its default state.
Now that you have got a clear idea of Manual and automatic redirection of the stream of Python programming, I guess we should do some examples.
Example of Automatic Redirection – Python Print function
Using these easy examples, we will try to understand this concept of Python programming more clearly.
Before we start I suggest you go through the previous tutorial first as we will be using various concepts from there. You can click here for that.
Step 1: Create A Python Script
As we will be saving the output returned by the Python Print statement into a file thus it’s a good practice to create a project directory which will contain all the files. By all the files I mean the Python Script file where we will write the code. And the file which will contain the outputs of the print statements.
In my case, I have created this directory into ‘My Documents’ folder and named it ‘Python Tutorials’. You can create your directory anywhere you want on your system and name it whatever you want.
Step 2: Create a Python Script
Once you are done creating the directory, next you have to create a Python Script. This Python Script file will contain all our Python codes. For the demonstration, I have created a Python File and named it ‘tut21.py’. Again you can name your file whatever you want.
Why we are doing this?
As I mentioned earlier, it’s always a good practice to keep your project organized into a dedicated directory. The major benefit of doing that is all the resources required by the project or created by the Python Code are at one place. It’s easier to find them.
In our context, the directory ‘Python Tutorials’ which I created in ‘My Documents’ folder contains the Python script. I will write the code for today’s demonstration into that script. The code which we will be writing today will create a file. Additionally, that file will contain all the outputs returned by all the Python Print function calls.
This code will create that file into the project directory which contains the python script file. Which in this case is ‘Python Tutorial’. That way we don’t have to do extra effort for finding the file created by the Python code.
Step 3: Write The Code For Automatic Redirection of Python Print
Now that we have organized our project, next we have to write the code. For that Just open the Python Script File which you just created and start writing this code.
import sys sys.stdout = open("Tut20_Output.txt", 'a')
This is the first part of the code for Automatic Redirection of Python Print function.
In the first line, I have imported the ‘sys’ module. ‘sys’ is an in-built module of Python libraries.
As you can see, statement 2 is an assignment statement. On the left-hand side of the assignment operator, we have “sys.stdout”. stdout here is a file object. If not set to anything then by default it sends the output to the default output stream. Which is almost always the screen of your system. But we can reset it.
On the right-hand side, we have the Python Open function. We already know that this function returns a file object of the file whose name we have specified into it.
On execution of this statement, the interpreter will redirect the output stream to the file whose object is returned by the Python open function. That file in our case will be ‘Tut20_Output.txt’.
Also, I have used ‘a’ as open mode (the second parameter of the ‘open’ function) thus the Tut20_Output.txt will be opened in ‘append’ mode. This means on each execution the data returned by the program will get appended at the end of the file.
You can read about Python Open Function here in detail.
As a result, on the execution of this statement, the interpreter will reset the default behaviour of the “stdout” from printing the output on the screen to saving it into a file. Which you have specified into the Python Open function.
print("Welcome To RebellionRider.com") print("Here We Make Learning Computer Programming Fun!") print("Make Sure To Follow Us On Facebook") print("www.Facebook.com/TheRebellionRider")
Here is the second part of the Python Program. The second part contains only Python Print function calls. If you will read them carefully then you will find out that I have not used the ‘file’ parameter in any of them. Ideally, on execution, you should see their output on your screen, as it is their usual behaviour.
But here on execution, you will find out that, the output will not be there on your screen rather it will be saved into the file.
Why is that so?
The second statement (check part 1 of the program) of this Python Program has changed the output stream from printing the output on the screen to saving it into the file. Hence no output on the screen this time.
That is how we redirect the output stream in Python Programming. Hope this demonstration helped with your doubts. But still, if you have any, then feel free to leave me a message on my Facebook.
Now let’s come to the second part of the tutorial – resetting the output stream to its default state.
Watch The Video!
If you learn faster by watching video tutorials then just for you I have done a video. Check it out
Reset The Output stream.
Suppose, you added one more print statement to your program and you don’t want to save that into the file. Rather you want to display it back to the user. For that, you have to reset the output stream to its default behaviour. That default behaviour is to show the output on the screen.
To reset the output stream in Python programming you have to follow a two-step process. Let’s see what it is –
Step 1: Save The Default State of Python Print Into A Variable
Firstly, you have to save the default state which is ‘sys.stdout’ into a variable. Also, make sure that you must create this variable before the statement which contains the Python Open function call. Which in our case, is the second statement of part 1 of the program.
Step 2: Assign that variable into the ‘sys.stdout’
I know it sounds confusing but that is how it works. You cannot assign sys.stdout into sys.stdout, like this
sys.stdout = sys.stdout
I have demonstrated and explained this in the video tutorial, which you can check here. This thing won’t work.
The proper way of resetting the default behaviour is to store the state into the variable and then assign that variable into the ‘sys.stdout’. Like this
import sys restorePoint = sys.stdout sys.stdout = open("Tut20_Output.txt", 'a') print("Welcome To RebellionRider.com") print("Here We Make Learning Computer Programming Fun!") print("Make Sure To Follow Us On Facebook") print("www.Facebook.com/TheRebellionRider") sys.stdout.close() sys.stdout = restorePoint print("End Of The Program!")
The output of all the Python print function calls before statement 9 will get saved into the file whereas the output of all the Python print function calls after statement 9 will get displayed back to you on your screen.
Thanks & have a great day!