How to open, read and write to a file in Python

As promised, here I am with the second tutorial of Python Print function. In this tutorial, I will explain the use of the fourth parameter of Python print function which is “file”. Here you will get the idea of Python File Handling in an easy way.

But before we move ahead I just want to remind you that this tutorial is in the continuation of the previous one where I explained to you the first three parameters of the Python print function. Thus, I highly recommend you check that tutorial first for a better understanding. Click Here to check the Python Print Function Part 1.

You will learn today…

Here is a sneak peek into what you will find in this tutorial—

In this tutorial, you will learn, how to –

  • Open a file using Python open ( ) function.
  • Redirect the output stream to save the output into a file

Aim of This Tutorial.

The main aim of this tutorial is to teach you how to redirect the default output stream of Python to a file. The default output stream of Python is the screen of your computer, which is why you almost always see all your outputs on your monitor. Let’s start the tutorial

What are streams?

You will find the concepts of streams when you study about the Operating Systems, especially about the Linux. But, if you ask me to explain the streams in just one sentence, Then I will say – Streams are like pipes through which sequence of bytes flow in and out.

Problem with the default Output Stream of Python Print Function

By default, the Python Print function is designed to show the output on the standard output stream. Which is almost always the screen of your computer.

For the demonstration, go ahead – copy and execute these Python print functions on your system.

print("We all talk Python with RebellionRider") 
print((12*9/5) + 32)

Now you asked, what’s wrong with the output of these python print functions?

There is nothing wrong with this output. But question here is will this output retain itself for reuse if we do some changes in the function call.

For example, if I modify the expression and change the multiplication factor from 12 to 12.5 in the second print function call. Thereafter I want to compare the results of both the expressions for analysis.

print((12.5*9/5) + 32 )

Apparently, the execution of the modified Python Print function call will produce the result but at the same time the result of the previous Python statement will be lost. And, because of that we cannot compare both the results and perform any analysis.

Solution of the problem.

Now that I have explained, what is the problem with the default output stream of the Python print function, let’s talk about the solution.

Wouldn’t it be awesome if we could somehow modify the output stream from printing the output on the screen to saving the output into a file permanently? That would solve our problem, right?

Saving the output produced by python print function into a file permanently is the solution of the above stated problem. Let’s learn how to do that.

Advantage of saving the output into a file?
The main advantage of saving the output into a file is that we can reuse it anytime we want.

Python Print function – File Parameter

We take help of the fourth parameter of the Python Print function which is “file” to redirect the output stream. The default value of the “file” parameter is set on sys.stdout. ‘stdout’ stands for standard output. stdout always points to the standard output stream which is almost always the screen of your computer.

The default value of the file parameter is sys.stdout.
print(objects, file = sys.stdout)

What can you assign to the file parameter of Python print( )?

Firstly, you can only assign an object to the file parameter. Secondly, only those objects which have access to the write(string) method can be assigned to the file parameter.

Example of file parameter of Python Print function –

Below given code will create a file and save the output of the Python Print function into that.

fileName = open('PyTut20.txt', 'a')
print((12.5*9/5) + 32,  file = fileName)

In the above code we have two statements, let me tell you what is happening in those statements –

Statement 1: In the first statement I created a variable and named it “filename”. This variable will hold the file object returned by the function open( ). You can read more about Python open( ) function here.

Statement 2: In the second statement we have the Python print function call. This is the same function call which we have seen above except the added file parameter.

On execution, the interpreter first evaluates the mathematical expression and then it looks for the file “PyTut20.txt” in the project directory. If it finds the file there then it will save the result of the expression into that. If the file does not exist, then it will first create the file with the name “PyTut20.txt” and then save the result into that.

Is this redirection permanent?

No, this redirection of the output stream is temporary. Which means if you write a print statement without the file parameter, next to the one which we just wrote. The output of that Python Print function will get displayed onto the screen.

fileName = open('tut17.txt', 'a')
print((12.5*9/5) + 32,  file = fileName)
print("I will be on your screen on execution")

However, the output of the first python print function call will get saved into the file PyTut17.txt but the output of the second print function will be displayed back on the screen.

To learn how to permanently redirect the output stream of the print function, stay tuned.

So that’s it for this tutorial on how to use the file parameter of the Python print function to redirect the output stream. Hope you enjoyed reading. You can reach out to me on my Facebook Page for more discussion on the topic.

Thanks and have a great day!