The Python computer programming language is so rich in features that it offers many ways to accomplish a specific task, but based on my personal experience with it, only a few ways are being used and accepted by those who get paid to write professional code daily. Not that it is totally wrong to write the code in your own way, but it is highly recommended to stay updated with the best practices which very skilled computer programmers make use of.
Through this article, you will learn how to properly open a text file so you can read and write data. Before going any further, make sure to open a new interactive console in your own operating system with the main purpose of practicing the code being shared in here, by yourself.
First of all, we will show you how to read and write a text file in a wrong way. So, run the following piece of Python code to create a new text file in your own operating system.
f = open('text.txt', 'w')
According to the official Python documentation, the above code opens a new text file in writing mode. As you have probably guessed, the ‘w’ stands for writing mode.
Now that you have managed to open the text file, you can easily write data to it by making use of the following function.
To check the kind of object which you have already created in your interactive Python shell, just type the following command.
Once I executed the above command in my interactive Python console, I got the following output displayed.
<open file 'test.txt', mode 'w' at 0x107662420>
As you can see from the above output, the file is opened in the mode ‘w’, a mode which means writing.
Now close the file by making use of the following piece of Python code.
Once the file is being closed, the Python coder can easily read data from it by opening it in reading mode first.
Now that the file has been closed, open it in reading mode by making use of the following command.
r = open('test.txt', 'r')
Based on my personal experience with the Python computer programming language, the above piece of code opens the file which we have already created, in reading mode.
To read data from the file which we have already created through the Python computer programming language, the following piece of code can be used.
Once the above piece of Python code got executed on my own interactive shell, the following output came out.
By making use of some of the utilities being offered by default in the Python computer programming language, we managed to open a new text file for writing and reading.
So you may ask, what is wrong with the techniques being used above? Based on my personal experience with the above techniques when working with files, there is a common issue with them. When opening files by making use of the above techniques in the Python computer programming language, most of the time the coder forgets to close the file once they’re finished with it.
As you saw earlier in this article, we managed to close our file by making use of the following command.
Having worked with a veteran software engineer for some time, I have learned that the best practice to deal with files in the Python computer programming language is through the builtin with statement. According to the official documentation, this statement is designed to provide a cleaner syntax and a better way to deal with exception handling.
Based on my experience, the with statement closes the opened file automatically after the coder is done working with it. To illustrate the theory with some Python code, take a look at the following. It shows the syntax for the Python with statement.
with open('file.txt') as f: # do something here
Now let’s make use of the Python builtin statement called with to open a new file, write data to it and then read data from it.
First, we have to make use of the Python with statement to open a new file like shown in the following example.
with open('newfile.txt', 'w') as text: text.write('This is a new file')
As you already know, the ‘w’ stands for writing mode. It tells Python to open the file for writing. We managed to write data to the file by using the builtin with statement.
I mentioned in this article that the with statement closes the file object automatically once the coder is done with it. You can easily check if the file which you have opened above, is closed automatically or not by the with statement. All you have to do is to run the Python command being shown below.
Once I executed the above command in my Python interactive shell, the following output was produced.
<closed file 'newfile.txt', mode 'w' at 0x0000000002D60C90>
As you can see from the above output, the file has been automatically closed by the Python builtin with statement. How about reading data from the file that we just created?
It is very simple. All you need to do is to open the file in reading mode. The following syntax can be easily utilized to open the text file in reading mode inside the with statement.
with open('newfile.txt', 'r') as text: text.read()
Once the above piece of Python code got executed in my interactive console, the following output was produced.
'This is a new file'
As I mentioned in the beginning of this article, there are many different ways and techniques to accomplish a certain task in the Python computer programming language. Although doing stuff your way may not be necessarily wrong, it is always a good idea to follow the best practices used by professional software developers.