Transcription
Hello! In this lesson, we will study a manager called "with," which is used for working with files in Python.
Before we begin, I would like to recommend the website "kipr.com." On this site, you will find your homework and a lot of other useful information. The link to this lesson on the site will be in the description of this video.
To understand the advantages of the "with" manager, let's first work with files without using it.
So, let's create a variable, which we will call "file," and here we will simply open a new file. We will open it for reading; let's assume this will be "text.txt." In the mode, we specify "r." I want to remind you that the "r" mode means "read," and this mode does not allow us to create a file if it does not exist. The "w" mode allows us to create a file if it does not exist, but the "r" mode simply reads data from the file, and if the file does not exist, an error will be raised.
Let's try to raise this error because I do not have such a file here. I will try to raise the error.
Now, let's possibly try to access "file.read," and at the end, we need to close the file, so we will also use "file.close."
If I run the program now, we get an error: "FileNotFoundError." This error is related to the fact that we tried to open a file, but such a file does not exist.
To handle this error, we will use the "try-except" block. First, we will use it, and a little later, we will look at using "with."
In the "try" block, we write the code to open the file, read information from it, and close the file. We also need to write "except" to catch the error called "FileNotFoundError." Here, we will simply write that the file was not found, something like that.
If I run this, it seems everything is handled correctly, but we have a small problem. The problem is that the error was raised at this point, and consequently, all the other lines of code were not executed.
Thus, at this moment, our program started, and there was an attempt to open the file, but it was not successful. We still need to close this attempt to open the file, and this line of code did not execute.
To ensure this line of code executes, we need to add "file.close" here, or it would be more convenient to add a "finally" block, and in this block, we will simply write "file.close."
So, we are trying to close our file, but here, when we run it, we will encounter a problem. The thing is, this variable "file" is simply not visible in the "finally" block. We created it in the "try" block, and it is only visible there. It is not visible in the "except" block, nor in the "finally" block.
For this reason, we are trying to access a non-existent variable and call the "close" function, but since the variable does not exist, there is no "close" function for it, and this causes an error.
So, let's use the "with" manager now. Let's remove everything in the "try" block and also remove the "finally" block, and simply write the "with" manager.
It is written as follows: we write the keyword "with," then we specify that we are opening a file. We will open a file that simply does not exist in our project. We specify that the mode for opening is "r."
We can also specify a third parameter, which is the encoding for opening the file, for example, "utf-8." This is the standard encoding that allows us to read or write both Cyrillic and Latin characters.
We also add the keyword "as" and specify an alias for our opened file. Let's call the alias "file," although it can be named anything.
Here, we simply access "file" and see what this construction gives us. The thing is, when we use the "with" construction, we immediately state that this file will be closed after opening.
Therefore, we do not need to write any additional "finally" block because the file will be opened and then closed automatically.
If we run this, everything is handled correctly. If the file is not found, an error is raised, and we see that here.
If the file is found, let's create it with the content "Hello." In this case, we will simply print the information from this file.
Now it works like this. The "with" manager is the ideal manager for working with files because if it weren't for this manager, tracking errors related to files would be quite difficult.
But with its use, everything is done as simply as possible. The "with" manager opens and automatically closes the file.
As you can see, I did not call "file.close" anywhere because it is simply not needed. The manager closes the file upon completion of its work.
As soon as all these lines of code are executed, the file will be closed, even if an error occurs. This is extremely convenient.
Well, our lesson is coming to an end. In this lesson, we learned how to work correctly with files using Python.
I hope you enjoyed the lesson. If so, please subscribe to the channel and join our social media. All the links will be in the description of this video.
That's all from me. See you soon! Goodbye!