Hey guys,
welcome to Free Tutorial TV and Python tutorial for beginner.
In the last video we have seen how to create a file in Python. and how to write content in that file in python.
now in this video 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 video . I have told you that you can use this open function to open any
text file . and this open function takes two arguments.
First is the path 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'm 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 back slash 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 video when we write w here or A here that means you want to write or append
to the file . in order to read from the file.
This mode here should be R.
Which is R read mode . in the last video we have seen this table in which we have seen that we can
use this R or R plus mode in order to read the file.
Or if you want to read and write at the same time you can use this R plus 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 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 see if you provide some file path which doesn't exist.
So this file 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 ad says that no such file or directory.
So if you tried to open a file which doesn't exist it will through 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 read the 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 full list of first line here.
So let's say I want to just read the first four character of this line one. then I can just write for
here which will read the first four character of the line 1.
Now if we just want to read the first two words of this line one. then you can see the number of character
here.
4 for this.
And then there is a space of five character.
**************************************************************************
please like and follow us in Facebook and Instagram.
facebook: / technologytv
Instagram: / heikhamsingh
Twitter: / hanandkumarsing