Python Tutorial for Beginners 39 - Reading Files in Python



 hey guys welcome to the next blog in Python tutorial for beginners in the last blog we have seen how to create a file in Python and how to write content in that file in Python now in this blog I'm going to show you how you can read the content of a text file using Python now if you remember in the last blog I have told you that you can use this open function to open any text file and this open function takes 2 arguments first is the part of the file with the exact name of the file so because this demo dot txt file is in the current folder in which I am working in that's why I haven't provided any exact part I can just give the file name here but if you have the full path then you can provide for example C colon backslash and then whatever is the folder name and then the file name so either you can provide the exact path of this file with the file name or you can provide the file name if this file is present in the same directory in which your Python script is there now the second argument which this open function takes is the mode of opening this file so we have seen in the last blog when we write W here or a here that means we want to write or append to the file now in order to read from the file this mode here should be R which is the read mode in the last blog we have seen at this table in which we have seen that we can use this R or R + mode in order to read the file or if you want to read and write at the same time you can use this R + mode now if you see here I have also written here that R is the default mode so when I don't give any argument here as the second argument which is for the mode then also the default mode is the read mode so let's see how we can read this file which is demo dot txt file and the content of file is shown here so in order to read the file you'll use the file handler and then use the function for example read which is used to read the whole file now in order to print the content of the file you need to use this print function so this FH dot read will provide the string and then using this print function we can print that string so let's run the code and let's see what happens so you can see it prints the whole content of the file now let me minimize this window once again now let's say if you provide some file path which doesn't exist so this file name doesn't exist in the current folder and when I try to read this file let's see what happens so it's going to throw an error and this error says that no such file or directory so if you try to open a file which doesn't exist it will throw an error so you need to give the exact name of the file in order to read it now here we can also be more specific and we can also say that this is our read mode in which I want to open this file and once again when I run the code it's going to read the content of this file now let's say I just want to read the first word of first line here so let's say I want to just read the first 4 character of this line 1 then I can just write 4 here which will read the first 4 character of the line 1 now if we just want to read the first 2 words of this line 1 then you can see the number of character here 4 for this and then there is a space so 5 character and then I ask is 2 character more so 5 + 2 7 so when we write 7 here then it's going to read the first 2 word of this line 1 now let's say I want to read the whole line so instead of some words or some characters I want to read the whole line then I can use a function called read lines and this read line function is going to read the first line of the text which is present in my file so I'm going to just run the code and you can see it reads the first line of my text file now if you want to read the next line then you need to use this read line function once again and when I run the code once again it's going to print the second line and so on so if you want to print the three lines then you use this function three times now let's say you want to read some characters of the line one then you can once again write the number of character which you want to read and then run the code and it will print the first four character of the first line and you will observe that when I use this read line function once again the string starts from the ending of the first line so in the first line we have just used the first four characters of the first line now pointer will be set to the end of this first four characters so when you use the read line function once again it's going to start where the first line has ended so first line has ended at the end of the fourth character and then it's going to read the space and then all the line characters which are there so in the second read line statement it has printed this kind of text which is after the first four characters and in the third read line because we haven't provided any argument here so it has printed the whole line here so let me minimize this now let's say you want to read all the lines of this file in the form of the list so you want to read the ten lines of this file in the form of list you can use a function called dot read lines so here you just need to write dot read lines and this function is going to give you the list of every line in that text so let me run the code first of all and you can see this is the list which starts from a square bracket and the first element of the list is the first line with the backslash n which is the line break comma the second element of the list is the second line of the text which we are reading from here so now we know that this read lines function returns the list so we can also call some index of this list so let's say I just want to read the fifth line of this text then I can just write index four because index starts from zero right so let me run the code once again and it prints this line which is at the line five now let's say I want to read the last line I can just say nine and it's going to print the line number ten of your text file let me minimize this once again now let's say you want to read all the lines of this text one by one and do some operations on each and every line one by one so you can use a for loop for that to iterate over the lines one by one so for let's say line in your FH which is the file handler and then you can print the line one by one so you can call this variable which is line here and now let's run the code it's going to iterate over this text line by line and every line is printed one by one using this print line statement so let's say you want to count the number of characters in every line you can use this function which is le and function which is used to count the length of a string and when I run this code now it's just going to print the number of characters in every line now once again let's say you want to count the number of words in every line so we can use a function on this line variable which is a function called split so line dot split which is going to split your line on the basis of character you provide as an argument here so let's say I want to split the line on the basis of a space so let me run the code once again and now you will see that it's going to give me the list of each and every word of every line so this is the first element of the first list and then the second element third element and fourth element and the fifth element so if you want to count the number of words in every line then once again we can enclose this code in to a function called le n okay so this length is going to give you the length of this list and when I run the code once again it's going to print the number of words in every line of this text file so if you want to count the number of words in every line you can split the words using the space and then using this Eliane function you can just find the length of the list which is returned by this split function or if you want to just see the list of words in every line then you can just use this function without the Eliane function and it's going to give you the list of each and every word of every line of your text file and of course to make things easier you can also use this with notation which I have also told you in the last blog so you can write with and then this open function and in the open function you give these two argument which is the name of the file and the mode and then you can use as a fetch for example and then you can execute whatever code you want to execute using this file handler for example once again I want to read the content of the file once again so using this width statement you don't need to use this FS not close function because this width function is going to take care of closing of this handler by itself so this whole code is equivalent to the code which you write whenever you use the try finally block with this code also so this is also possible so let me just remove this code and let's run the code once again it's going to give me the same result as we have seen earlier so in this way you can read the text files in Python I hope you have enjoyed this blog and I will see you in the next blog  

Post a Comment

Previous Post Next Post

Recent in Technology News