Transcript for:
File Handling Techniques in Python

In the previous video we saw the basics of 'file.io' we saw how a basic file handling is done in python Why the file has to be closed, how to open the file, what is the open method? In today's video, we will see some more interesting methods while continuing this discussion. as if 'Read lines', 'Write lines' Let's go to the computer screen And let's get started So sometimes what happens is that we have a lot of scientific lines in our code and we want to read all those lines In this case we can use 'read lines method' so here I will simply show you first create a 'myfile.txt' 'myfile.txt' I'm going to write inside, Harry bhai python course is great let me correct the spelling of it Otherwise, people will think that I don't know how to spell it which I do well, what am I going to do here? I will tell you the use of read line method well, why read line method was made? read line method was made so that you can read any file line by line. It is simple. What did you do, you wrote this by writing f = open what are you doing now you are reading a file till there is a line in it as simple as that which will be line at a time I do one thing here simply what I do is print the line first so that at the end you can see what is the value of the line So, you see here the value of the line now what is happening that I have to do this indentation what I have done this I should have done the indentation properly never mind, never mind we all make mistakes so here you see 'Harry bhai python course is great' came And then after that the line that I print came blank now here, if not line I do one thing I do print line and type of line And you see what the value of line is so, look here, when I print line and type of line so, it becomes an empty string. At one time the line which is there is not found at all and what will we do at that time that we will stop reading it So, if I revert my program again on this program then you see what will happen that we are reading line by line from 'myfile.txt' But our 'myfile.txt' had only one line so, that's why it was not fun and I write here, And this tutorial is also awesome my friend is getting cold, my fingers are freezing, ok Don't judge at all now Now I will write here, This is also good and I am writing only small, not planning to write much longer. But it's not that I don't feel like writing code my fingers get hot when the time comes to write code. Now look here one by one what we are doing is printing the line 'Harry bhai python course is great' the second line is printed then the third line is printed if I want to do something that requires me to read line-by-line then this is very handy we have to do this a lot of time We sometimes have to read the values ​​which are in one line each and parse them so, we can do that We are given a lot of data like if you people will do machine learning data science then the data that is given to you on Kaggle etc. It goes something like this let me show you a very cool use case which will give you a lot of fun like let's say I have 56, 45, 67 12, 34, 63 and similarly, I have this over here, ok Let's say it's someone's marks 3 subjects have 3 students' marks, ok and I want to write it well so, I can do something like All I can say is look the line it came to me And I'll say that, m1 = and I'll indent it a little bit I've double indented it By doing 'm1 =', I will write 'f.split' and I'll do something like this Split by (,) and I will say 0 This is mark 1, ok similarly marks 2 and 3 will also be I'll do this 1 I'll do 2 and here I'll do 'm2' and here I'll do 'm3' and then you see I'll print 'm1, m2, m3', ok And I'll say, "marks of student 1 in maths is: and here I'll put 1, ok let's say the first mark is written what it is written? marks of maths And first I have marks of other subjects also like let's say Science marks of student 1 In fact, not of student 1 I would say of student {i}, ok I do student {i} you will say what is {i} what is {i} let me tell you what it is Give me 1 minute, I will tell you what is it {I} will be my student means the line that will be going on so what will I simply do here I will do 'i = 0' And here I'll do, 'i = i + 1', ok And what will happen by doing this simply first here the value of {i} will be 1 then it will be 2, then it will be 3 as the loop goes on as the lines will be detected Now I put 'English' here and 'SST' here, ok 'SST' has only 3 characters I am a bit lazy, so I took this subject, ok If you know any other subject, which is smaller than 'SST' then tell me in the comment I don't think there is any other subject like this. Which is written in less letters than 'SST' tell me in the comments if you have in your mind I can't think of it Now here's what happened 'f.split' while I'm doing in fact, a thing I've got to put up in that case Because what is happening is reading the line one by one and at some point, the line will be empty but I have to read the line first so, I have to put it up so, what is happening now if not of line I have messed something up here seriously what is happening I did f.split(",") it is saying that Why did I do 'f.split(",")' I should do 'line.split' what is this? this is called programming you do mistake, error comes Again you make mistake, again error comes so, look what happened here there were the marks of first student if our data was formatted like this marks of first student are '56' then only '56' is coming after that what kind of joke is this I am saying look, this is called programming I have written only 'm1' I have not written 'm2, m3', ok so, this is called programming. that first you will make a mistake then you will know that you have made a mistake then you will correct it until everyone is satisfied literally everyone, ok '56, 45, 67' then after that '12, 34, 63' then '13, 64, 23' Make sure you keep this thing in your mind that this is the marks is called a 'string' means now what is being printed is a 'string' So, if you want to convert it to an integer, ok So, you have to use 'typecasting'. If you want to convert to float say this is marks out of 50 and I want to calculate its percentage. So, I have to double them to get the percentage what percentage is this so, if I do something like this here, I'll do *2 here So here I'm pressing 'Alt' on my keyboard then I'm making multiple cursors over here I'll do it if I do it like this So, you see here, "Marks of student 1 in maths is:" "Marks of student 1 in English is:" "Marks of student 1 in SST is:" so, it's going to be double-double which is wrong 1, 2, 1, 2, 3, 4, 3, 4 why is it coming like this Since this is a string what do I have to do either i typecasting here something like this I would say typecasting it in such a way that convert this thing into an integer I would *2 the integer. I will get the value properly, if I *2 the string then it will come 2 times so, my desired result will not come, ok Is it simple? very simple, ok What do I do, why did I double click on this what am I doing I do it like this, ok I close this sidebar And now let me tell you about the 'Write Lines' method 'Read Lines' method so you have seen it The writelines() method in python writes a sequence of string to a file, then what if a given to you a iterable object you want to paste a content inside it in a file, ok Let me tell you this program is very well written so, I will comment it out I will start writing from below suppose I have 'myfile2.txt' I haven't made it here yet but this program will run as soon as it is made see it is done now let's see what will come inside it, line 1, line 2, line 3 and a blank Why this blank has come because what happened here is that first 'line 1' came then 'line 2' came, then 'line 3' came (/n) was written at the end So that's a new line, ok and I'm going to use the 'write lines' method here so, what will it do with the lines, which is an iterable object will paste all its elements one-by-one into that file, ok So, it will come in the form of 'line 1', 'line 2', 'line 3' okay, I hope everyone understands this, ok Nice, very awesome Now over here I did, f.writelines(lines) I did f.close so, my file went close, o that was all keep that in mind that writelines method does not add newline characters between the strings in the sequence So, you have to do this work yourself it is not that you expect a new line after 'line 1' no, it will not happen, ok you have to do it automatically So, this is how you can do If you want to add new line between the strings you can use a loop to write each string separately. or you can do it like what I did if you have a huge list In that you won't add this little bit to the elements one by one or else your list is coming from somewhere suppose it is coming from database etc. So, you'll have to use of write writelines isn't for you anymore, ok so hope you get the point I would like to tell you one thing that if you have not accessed the playlist then definitely access it. And if you review this course by going to the first video that would mean a lot to me then you open the first video and open the first video and write in the comments below. And do put your review here tell in the review how is this course So that those people who are new, who are coming to watch this course who will have a doubt that this is a free course So maybe it is not that much strong, that much concept is not told in it Remove their doubt a bit tell them how this course is what things are being told in it give a rating out of 5 They will know what is being told in this course whether they can learn or not I really want that you help those people who are about to start this course That's all guys in this video for now Thankyou so much guys for watching this video And I will see you next time.