Day-11 Python + MYQL FREE Sessions | File Handling in Python

Akansha Yadav teaches file handling in Python: how to open, read, write, and close files using the open() function and its modes (r, w, a, x). She covers the with statement to automate closing, different reading techniques, and Unicode errors, plus interview tips for developer and data-analysis roles.
Chapters
It is visible to you.
Okay, so today we will be learning if or anything is—
sorry, uh, yeah, today we will learn about file handling basically.
Hello everyone, just want to confirm, yesterday or day before yesterday, we don't have a class, right?
Yeah, am I audible to you now? And also let me know once the screen is visible to you. Okay, so now let's start file handling. Okay, so basically whenever you wanted to access your files in Python notebook itself and you wanted to make modifications in the file, whether it is your txt file or any other format of files. Okay, so now we are going to learn about file handling, and this is useful in for both the roles. Like if you are going for the developer role or if you are going for the data analysis role, then in both the roles data handling is important— file handling is important, sorry. Okay, so basic understanding you should have about file handling is that we are going to perform some operation on a file We will be opening the file within the Python, and then we will perform some operation on that file, and then we will close it. So simple operations we are going to perform— that we can create any file, we can open any file, we can read any file, we can write on any file, and we can close it. A simple way of handling files, okay? So let's say I have to work on files. So for working on file, I need one file basically. So simply what you have to do is you have to open the same folder, go to this New File option, and then create a new file. So let's say I'm creating new.txt. I'm just creating one Notepad simple blank file, okay? new.txt is my file. Now in this new.txt, what you can do is you can write some lines or some paragraphs or anything on this particular txt file. And then we will perform some operations. We will try to read this file using Python, then we will try to add few more lines using Python, then we will try to create new files, and everything related to file handling we will cover. Okay, so simply now what I am going to do is I am going to take some text from Wikipedia and I will add it up over here. Okay, so I'm just going to copy a few lines about Python itself from Wikipedia, and I will add it up over here in the same file. Okay, so now I have added— so in this file, if you can see where I have added some text, so I've added a text in 3 lines: line 1, line 2, line 3. And in these 3 lines, I have certain numbers also, and I have text also. I'm using special characters as well. Simple, like a normal PDF file, you can say, or a normal TXT file has been created. Now you have to simply save it. Once you have saved, you have to open— you have to create one new file. Okay, so let's say file.ipynb. Now, using Python, how you can perform certain operations on the file? Now, to perform operation, first thing that you have to do is you have to read that file. So you have to open that file in Python. When you wanted to open that particular file which we have just created in Python, then you have to use the open function. So simply you have to use open function. Within the variable, you have to open it. So let's say my file name is f, like file. So I'm going to use f as a variable. f is equal to open, which is the function. Now here you have to write down the name of that file, so it is new.txt. Now there is one thing to note. If you have saved this file and you are working under the folder and you are using the file which exists in that particular folder, then you will be able to open it. If you have that— but this file, let's say this file is not in C drive, but this file is, let's say, in D drive or in Downloads or in desktop somewhere else on your system, then what you have to do is you have to simply go to that file, right-click on it, and then go with the Copy Path option. Once you will go with the Copy Path option, then it will give you the path in something like this. Let's say F1 is equal to open. This is the exact type of path you will get, okay? In this, there is everything is mentioned, like this is from C drive, this is under this user folder, then all, everything will be there. But if you are working in the same folder, then you can directly write down the name of that file. So for me, I'm working in the 30 days folder where I have the file new.txt, and I can simply write new.txt and it will open that for me. But if you are going with this method, then also it will work. Is there any question up till this? Oh man, okay, so now I'm going to just execute it. Okay, so there's a high probability that you will get this error, which is syntax error, but along with syntax error it is mentioned that it is Unicode error. So now in this case what you have to do is you have to add R. Over here. And now if you will run, you will be able to execute it properly. Okay, R stands for when you wanted to open that file within Python, then you have to use R before, uh, the file name. Simple. Okay, so I have two variables, f and f1. In both of the variables, I have opened the file. Now once you have opened, the second, second thing to note over here is you have to decide what type of operation you want to perform. Is it like you wanted to just read, or is it something like you wanted to go with the editing that file, or you wanted to remove every content and you wanted to add fresh content in that file? So you have to decide which operation you wanted to perform on that file because there are multiple modes available to open file. So within the open function, there are two parameters required. So just like in the functions, right, we have already discussed about function, that what functions are and how parameters work. Same way, in this, in built-in functions also, you have to pass the parameters. So there are two parameters which are required for open function. Okay, so when you're writing open, first thing that you have to mention is the file name. Second thing that you have to mention is the mode. So we have already written file name, but when you want it to be specific that this is the operation that you want, then you have to go with the mode. Now you can see when I have written mode, it is showing that this, this kind of information for me, right? And it says that you can use R, you can use encoding if there is something specific in your files, then you can use this. So there are multiple modes available, okay? So R is for reading. When you wanted to just read your file, then you have to go with the R. When you wanted to write something, when you wanted to delete whatever is there in the file existingly, then at that time you have to go with the w. Okay, so first is that you have to— when you're writing mode, it will ask you for the modes. You have to mention, you have to specify which mode do you want. So there are like— I'm going to tell you about few modes. First is R, which is for read. Second is A, which is for append. Third is write, like W for write. And when you wanted to create, then you have to go with the X. Okay, these are the 4 basic modes that you should remember. These are the 4 different modes, you can say, or different methods for opening the file. R, W, uh, X is for create, and then we have A. Okay, A is for append. So as we have already discussed that what append is, append means adding something to the end of the file. Okay, and this R is for read, then we have W which is for write, and then we have X which is to create file. Any question in this?
No, no.
Okay, now in addition to this, in addition to these 4 modes, what you can mention is you can mention the type, like if your file should be handled as a binary mode or your file should be handled as a text mode. You have to decide which type of file you are trying to open. Suppose you are trying to open an image. When you're trying to open an image, you have to go with the binary mode. But when I am opening something like txt file or PDF file or docs file or something like this, then you have to go with the text mode. So you have to use t— you can use t as a keyword for text. Okay, T is for text and B is for binary. Okay, so when you are handling images, videos, something similar to these, then you can go with the binary mode. But usually we will work with the text until and unless you are going for the computer vision role, then you will be required to work on the binary modes like images and videos. Otherwise you won't be. Okay, now how you have to mention it after writing this, let's suppose I'm going to just copy this entire line, and I'm going to add one more code cell, and I will enter it over here, making the variable changes from F1 to F2. And after this, you have to add comma, and then you have to mention the mode. Now I have written R. When I'm going to type R, it will show some options to you. Like, if I'm adding— like, you can see I've just added double quotation, it's giving me option A, B, AT, BA, BR. All these are the modes available. These all modes consist of two types of pattern. First is this: out of these four you will find something, and out of these two you will find— like you can see there, if there is T then there won't be B, if there is B then there won't be T. It means we are mentioning that you have to handle this file as a binary or you have to handle this file as a text. So these all are the modes. You can choose out of these modes as well. Okay, these are the different— yeah, up till here you will find modes. Okay, now if you wanted to read any description about what this particular mode is going to do for you, then you can click on that, uh, the icon, and then it will give you the description about that. Yeah, go ahead. Yeah, you want to ask something? Okay, so over here you have to mention. So just like, let's say I'm going with a very basic one which is read. I have created f2 variable where I have opened my file with the R operation. It means I am going to read my file. When you wanted to read your file, then you have to open it with R and then Next thing is, for making it display within Python, you have to use read function— read method. Read method will help you read the file content. So now I'm going to add another cell, f2.read, and this will read the entire file for us. You can see which the content which I have added in this file, I am able to read it over here. That's how you can read it. Now let's say you don't want whole PDF or whole TXT to be there, you just want limited number of characters just to maybe view which type of content that file consists of, then you have— you can mention one number within this bracket. So if I'm going to say 10, okay, there is one more thing to note. Let's say you have created one variable and you are performing some operation. Whenever we work on file handling, there is a sequence of steps that you need to follow. The steps are First, you have to open the file. Open the file with the correct mode. Okay, then second is perform operation. Third is close the file. Got it? If you are not going to close the file, you will find glitches because this is something which is already in Python. This is not gonna work as a library. This is just the inbuilt function of Python, and we are using those inbuilt function to make file handling. Okay, so this is not a separate library which you have to import, which you— which has versions. No, this has nothing. This is just the basic operation, basic functions, basic inbuilt function that is helping you perform file handling. Okay, so you have to follow these steps. First is you are opening the file, perform operation, and then close the file. If you are not going to close it, you will find some glitches like we were finding, like when, when I was writing 10, I was not getting the results. That is because you were— I'm— we have, we have opened the file once within the read mode and then we are performing multiple operations, but not— this is not allowed. Okay, so when you are going with the file and you are opening the file, every time you have to reload that file once for you and then only you can read it. Okay, so now let's say I'm going to run this again and I will just— so this will— if I'm writing 10, it means it will count from the beginning, like from first to 10 it will display. But I'm going to run it again, then you can see next 10 are displayed. If I'm going to run it again, next 10 again will come up. Next 10 characters will be added. Got it. So every time you have to make sure you are opening your file within the correct format and you are following the correct pattern to do that. Now once you have worked on that file, once you have read that file, whatever content was there, you have to close it. So simply, just like we have open function, we also have close function. So f2.close and run it. That's it. It means now your file has been updated with whatever changes you were supposed to make, or whatever operation you wanted to make, you have made it successfully. Okay, so that's how you can read anything from the file. When it comes to read anything from the file, it means if you wanted to read entire content and go with the read function, if you want specific numbers of characters, like you know that how many characters exactly you wanted to return, then go with the read function with one parameter, which is going to be a numeric value— 5, 10, 20, or 7, 8, 9, any number it could be. So you have to mention that number. Next thing, let's say I don't want to go with the characters, I wanted to read one line. Okay, so in that case, suppose I'm going— I have to reopen it, and I am going to use another function to display first line. So there is a function called readline, and then it will display first line for you. You can see we are getting first line printed for us. If you want 2 lines, if you will mention the characters, and if you will mention the number, it will take it as a character. Okay, when you wanted to read 2 lines, then again do one thing, just simply remove this and copy this particular thing. I'm going to run this. You can see you are getting the first line printed. Okay, I will just reload it after closing it so that we won't face any glitch. So let's run this. This is closed now. I'm going to reopen it within the same variable. I'll execute it. Now you can see you are getting two different lines printed for you. When you want two lines, you have to run both of the code cells together. Like, I have written both the print statement in the single cell. If you will do it like this, then you will get the results like this. Even if you are going to do it in the next line, then also it's fine. It is not necessary to, to be like within the single line. But when you want more than one line, you have to write down that function again and again. It's not like you will use these brackets to mention the number, like saying 8, I want 8 lines. No, the numbers which you are going to mention will be considered as a number of characters. Not as a number of lines. So when you are handling— when you are going with the lines, then you will go with the readline function, and you have to reuse the readline function as many times you wanted to print the lines for. Any question in this? Okay, now let's say you wanted to go with the line by line, you want each and every line to be printed, then what you can do is you can simply iterate over the line. So you can use for loops for this purpose. So let's say I'm going to use again a different variable. Let me just copy the same path, and I will just change the variable from F2 to F3 so that we will have the different variable. And before I begin with reopening, I have to close that file. So that one I have closed. Now this is the— again, I'm going to open it. For i in f3, print i. So now you can see, this, this way also you can read the entire content from the file. I'm not using any inbuilt function, I'm just using for loop for iterating over the characters and from the file, and I'm getting it on the screen. So it is advisable that you close the file every time when you are opening it, and when you are done with it, you just close it with the close function. So f3.close will help you close it.
Wait, ma'am, we can also add or update any file like Excel, PDF, or only we can do with this Word file or text file or Word file only?
No, it, it depends which type of file you are dealing. There are different ways to handle it.
Okay, okay.
Yeah, any other question?
No, ma'am.
Yeah, so simply you have to follow this way that you have to just close it every time. But let's say like this is very systematic that you have to open it, then you have to close it, and if you are not closing, you will find some glitches or you will encounter some errors, or maybe you will get incorrect data printed on your screen. So there is an alternate, obviously. So alternate is that you have to open that file with the— with method. Whenever you are going to use with method, then in that case you need not to mention— you need not to close the file. So how you will have to do it? So in this case also you have to use open function, but it goes with the with. So you will write like this, and then you will mention the path. So let me just take it from here. So with open, and then you have to mention the variable as, let's say, F4. So with open this as this, this will open your file, and then whatever, whatever you wanted to perform on that file, you can Continue to perform. Any questions, Ankit? Okay, now let's say I wanted to write on that file. Okay, so there are two writing methods, like there are two methods using which you can write on any file. First is append, second is write. When you are going with the append, append will do what? Append will add whatever text you are trying to add to the end of the file. Write method will overwrite on the existing content. It means write method will delete everything from the file and then you can add the new content in that file. When you are dealing with new content, go with the write method. When you wanted to add up some content, then go with the append. Okay, so simply let's use, uh, first the write method. Okay, so this is going to clear everything. So for now I will just copy everything from here. And I will paste it for future so that we can copy paste again. Okay, so F4 is equal to open Okay, so f4.write, and I'm going to, let's say, adding new content in the file after removing the existing one, existing content. Okay, and then I will just f4.close. Okay, so we are opening the file with the write mode— read mode. That is why we are not able to write on that file. Okay, so first what I will do is I will uncomment this file, this line. I will run it, and then I will uncomment this one. And now I'm just— now you can see we are able to execute because now I am opening the file with the write mode and then I am trying to write something. Now we will check whether the lines which we were trying to add has been added or not. Okay, to just to cross verify, I'm going to use the read function on this. So I will just write print f4.read. Oh, oh, wait. I have to use the R mode. F4, dot close. Okay, now I'm going to just Yeah, you can see our file has been updated. Adding new content in the file after removing the existing one. So those lines which we have added from the Wikipedia has been removed now. So we simply have this single line content in the file. Now when I'm going to execute this, when I'm going to use the read function, and we— I'm, I'm just trying to read the new content from that file, you can see that we are getting the updated content in the output. That's how you can write on any file. When you're going with the append, so simply you can add some content. So I'm going to open the file again. F5 is equal to— I'll just copy everything and I will paste it. Instead of F4, I'm going to utilize F5 this time. Instead of write, I'm going to use append. Again, we have to change the mode because now I don't want to write, I wanted to append in the file. So simply using the A. So instead of this content, I'm going to use this one which we have just copied from the file and the Wikipedia. I will add it up over here. Okay, let me just check if we are opening any bracket in between somewhere. Yeah, we are. You can simply use the write method also for this. And that way you will be able to update your file with the new content. Okay, I need to refresh it, and once you will refresh it, this code will work for you. So now for today, what you have to do is you have to practice the file handling, and then we will discuss more in file handling that we have— how we can add more stuff, like how we can perform multiple tasks on the files. Okay. Yes. Yeah, yeah, tell me. Can you repeat? I can't hear you, basically your voice is breaking a lot. Yes, Anke.
Ma'am, I want to know, like, you are working professionally, right? Yes. So what kind of questions do they ask for Python, as a Python developer, or as a data analyst for interview?
So that totally depends. Let's say you are developing something using the framework of Flask or Django. Let's say when you're going with the Django or Flask or any other, uh, different library of Python, then they will be specific. They will ask you the questions related to OOPS. They will ask you the questions related to how do you deploy the and how basically you are using the libraries. But when it comes to data analysis and data science, they are going to ask what is EDA, how will you perform EDA, which are the function that you have used, or which are evaluation metrics you were using to evaluate whether your analysis is going good or not. So that totally depends which libraries you are mentioning, which skill set you are basically mentioning. So let's say if I'm in my resume, if I'm mentioning related to Power BI, So most of the question will be asked from the Power BI itself.
Okay, ma'am.
So if I'm going to mention both, let's say Power BI and Tableau, then they will ask that what are— what do you think, like which platform is good? Like Tableau you will go with, or you will— you want— you think that Power BI is the best? So they— their question depends on this resume that you are going to present in front of them.
So as a data analyst, they will be asking me regarding this NumPy. Uh, they ask for Excel also, ma'am?
Yes, they will ask whether you have created any dashboard on Excel or yet or not. If you have, that is good because that is the first step of data analysis. If you want to have the good foundational knowledge about data analysis, then you have to work on Excel.
Okay, ma'am, got this. So can you help me with some questions? Like, uh, like you are into this IT background only, right? So if you can help me with more questions like, uh, asking in interview.
Yeah, sure, I will share if there is.
Okay, ma'am. So tomorrow we will be doing this file handling only, right?
Yeah, tomorrow also we will be working on the file handling.
Okay, yeah.










