@worldmappin: Congratulations, your post has been added to ! 🎉 this post and your own profile on the map. Be part of the Worldmappin Community and join to get in touch with other travelers, ask questions or just be updated on our latest features.
#Learn #Python #Together | 🔥 Day 8 | #basics | #file #handling #read #write #append #Students by @techcoderlabz on 3Speak
▶️ Watch on 3Speak --- Learn Python Basics Together 🚀 | Day 8 Welcome back to our Python learning series! In today's video, we explore File Handling in Python, one of the most practical topics you...
Tags: #free #learning #students #python #coding #beginners #filehandling #programming #hivesuite
Hello everyone, welcome back to my YouTube channel. Today we are going to talk about file handling. So let's get started by, first of all, looking at the example file that we want to read. So here is the myfile.tec and in which we have written hello word. So to start with, we're going to open a file and then read it. So let's say over here, I'm going to say my file variable equals open, and in that, you just have to supply the file part. So over here it's in the save directory, so I will simply supply the file in over here. Now, the second parameter, what is the mode? In the pop-up, you can see there are so many options, but out of which, let's say we just want to read the file for now, and that's why we are going to supply. Now that we have, you know, open the file, how do we read the file? So for that, there is a function which is available on the file variable that we have just declared, which is this. My way, read is the function with which you'd be able to read it. And whenever you're done, make sure that you close the file. So with this, we have closed the file. So, line number one, we open the file, right number two, we read the file, and whatever value we get, we are printing it, and then on line number three, we are closing the file. Now let's try running this. So, right click. and then run hyphen file in terminal. When I do that, you can see the output. Let's try changing the value over here in the file. So we've added one more line. Now, let's try running this. So now if I run it again, you can see that the output is different. So this is how you can open a file, read the file, and close the file. By default, if you use R and then T, it means it's going to read the file as a text file. but if you supply RV, it's going to read the file as binary. So, like I said it earlier, if you don't supply anything, it's going to create the file as the text file. So in this example, we understood that, how to open a file, and at the end, like line number 3, you have to make sure that we always close it. So, let's say if you want to avoid closing the file, there's a difference in that which goes something like this, with, and then open the file. which is, in our case, my file, TXT, and then over here, we are going to supply as a readboard as file. So with this, we have declared a file variable, and then you can see the indent now. So basically everything that we provide within the scope of this section, it's going to have the scope of the file, and as soon as you are out of the scope, it's no longer available. So now... I'm going to print file dot ray and one more line which I'm going to add file is closed without close function because we kept the file within the width section so we can't the file within the scope and that's why the moment we are out of the scope the file is going to be closed automatically right click run and we can see the output twice over here and after that we can also see that the statement that we can see over here 5 was closed so in these two examples you saw that we were able to read everything from the 5 but let's say if you want to read certain number of bytes so each character represents single byte when we are reading a text file so let's say 2 byte if I want to read first 5 words so instead of say read which would read the entire file. I'm going to say 6 bytes. Now let's try running this. So you can see that it actually printed hello and space. Let's read 8 bytes so that you get even more idea. Hello and then space and then WO. So it's basically reading 8 bytes from the given file. Now the other thing that I want to highlight is how do we read line by line. So for that, we can use read lines. So for now, I'm going to copy this and put it over here. So this is one example. In here, I'm going to add the comment, read whole file. So this is read whole file example, and then this is read first 8 bytes or file. In this example, we are going to read lines. So, rate five lines. Now if I say in this one, rate lines, over here, in the output, we only see one line, because we said read line. So if I add one more statement, it's going to rate another line. Right click, run, and now you can see extra lines over here. I go back to this one and I'm going to say a third line and going back over here and adding one more statement and running the Python file and you can see three different statements over here. So this is how you read line by line when you are basically reading from a file. Now that we have covered how to read a file, let's talk about adding something to file or writing something to existing file. So I'm going to copy this, put it over here. add one more section in front and say append one line to existing file so we already have my file. txt so in this example i'm going to say a a means append more now i can say file dot write so with the append mode whatever content we have is going to add content on top of it so we're going to write let's say this is fourth If I try reading the whole file after this, it would read the fourth line as well. I click, run. And you can see that the fourth line was added, and if I go to the file, you can see the fourth line was added. Of course, it did not add the Slasson character because we missed adding it. So let me try, deleting that from here first, and then now if I return, with Slash and character in the beginning and then T capital. Now if I run this, you can see the fourth line was added along with the Slashen character and the fourth line. So this is how you append. Now if I keep running this over and over, then it would keep adding this is the fourth line over and over. So let's say I'm running this two times, then if it's adding the fourth line two more times. So this is how you append the content to existing file. Let's talk about the right mode. when you are right, opening the file with the right mode, it's going to override everything. So make sure that you have the backup of the file which is important, in case it is important. So I'm going to open the file as the right mode now. I'm putting the statement, I'm going to re -write everything. Let's try running this and then reading the file, try it up with that. So if I run the file, Python file which is files you can see that in the console output that it was printing up to I guess four lines of the port line but after that it only says one line and it says I'm going to rewrite everything and if I open the file you can see there is just one line and which was due to the right mode so whenever you are opening file with the right mode make sure that you have appropriate data and you write it accordingly let me know how to open a file, how to open the content to the file and how to write the content to file. Let's talk about how do we delete a file. In order to delete the file, we have to import OS. Once we have the OS instance or OS package available, we have a method called remove, and in that we just have to supply the file name. So I will supply my file.t, pacts t, and with that we'll be able to delete the file. So if I run this, right now, we do have a file over here in the directory, whenever the program is done doing everything, it's going to remove the file. Let's try running this. And you can see that file was removed. So this is how you can remove a file. But after this, what if I try to read the file again, or what if I try to open the file again? File doesn't exist over here. So if I try to run this, it's going to throw an arrow over here. Now, how do we handle this situation? So that's what we are going to talk about. Okay, so now we are going to talk about what if file doesn't exist at all. and let's see if there is no file there is no point of running any of this so for that you would make system as well and after that we're going to check if os dot path. exist in that we will supply the name of the file my file dot dxt double equals false so in this case i will print file not found named my file dot dxt and after that we can simply say okay system dot exit So with this, we are returning early because there is no point of running the code over here because file is not there, so none of the code would work. So let's try running this. So now I'm trying to run the file in terminal. When I click on it, you can say that it says file not found. But if I add the file with the name, file dot dhd, and add something in it, it's just going to work fine. And at the end, of course, because we have the remove statement over there with that it's going to remove the file after the program is done so it was able to read the file and then it removed everything so I hope that with that you have learned a lot a quick summary number one how to open the file and then how to read the file contain number two how to use the width scope and immediately close the file without explicitly saying to close the file how to read specific bytes how to read line by line, and how to append data to file, how to open the file in the right mode, and how to remove the file, and how to check if the file exists or not. And let's say if file doesn't exist, exist, so how do we hardly determine? So these are the things that we have covered today. I hope that it was helpful session, and see you in the next one. Thank you so much for watching. Bye-bye.
@worldmappin: Congratulations, your post has been added to ! 🎉 this post and your own profile on the map. Be part of the Worldmappin Community and join to get in touch with other travelers, ask questions or just be updated on our latest features.