Hello welcome to this tutorial on built in functions in this tutorial.
We will talk about the built in functions file input output print function input function.
These built in functions are very important to know and will be used frequently while coding.
Let's begin with the file input output function.
Python has built and functions to create and manipulate files.
Consider this function the open function this function returns a file object so to the open function
you need to parse the file name as an argument.
All other arguments are optional and have default values.
Say for example if the file that you want to open is in the current working directory you can just provide
the file name like this test dot the X D.
If the file resides in a directory other than that you have to provide the full part with the file name.
Make sure the file name and the PI given is correct.
Otherwise we will get a file not found error coming to the access mode the access mode defined in which
way we want to open a file if we want to open a file for read only write only or both say for example
if we want to open the file for reading.
In that case the access mode would be art.
And this is the default mode if you want to open the file for editing that is for writing will be the
access mode would be w.
And if you want to add text to an existing file you can use the access mode it append.
So I want to create a fly test dot the equity in the current directory.
So I'm going to use this function.
The open function if the file does not exist.
This function would create a new Y in the right more we have to be careful with the access more w as
it will overwrite into the file if it already exists.
All previous data would be erased.
So using the open function I'm going to create the file test data.
Dot the extremes in the right mode.
If the file you that you're going to create is in the current working directory you can just provide
the file name like I did here.
Or if the file decides in a directory other than that you have to provide the full part PDF file name
using the open function.
I'm going to create this file.
So let execute this line of code.
Okay.
Next and this is the file that God created test data the 60.
So we haven't written anything in this file yet so it is empty.
Now let's close this and write into some text into this file and for that you can use the right method
on the file object that we have here.
So in order to do that on this object if you need to call the right method so let's call the right method
and you can enter some texture.
So I'm going to enter first line backslash in the backslash character can be used to tell the interpreter
that the next character following this slash should be treated as a new line.
Now let's and third a few more lines into this file if the dot right let's say second line and then
one more line.
All right so I have endured three lines in this fight.
Let's close the file now.
The close method helps to free up the resources that were tied to defining let us now open the file
again.
Here is the text that we have endured using the right method on the file object.
Now let's add in more text to this file using the append method.
The append method does not or what.
The existing text in the file but keeps adding text to define it.
So let's close this and open the file again since it is closed.
Let's open the file again using the open method.
So the file name is in court.
That file name is test underscore data dot the 60 left open in the append mode.
So we have opened the file in the access mode append.
Now in order to write into the file we are going to use the right method.
I am going to enter this text into this file and then finally I'm going to close define.
After we're done let's open the file again.
The last line has been appended using the access mode a next.
You can also read what is there in a file using the access mode od which is read letters open this file
in the read mode.
So the file name is test data that the X T and then the access mode is odd.
Now say for example I want to read the first four lines in the file.
So I'm going to pass forward as an argument to this method read
so as you can see the first four characters in the file has been retrieved.
Now let's pass forward as an argument again to the Reed method.
The next four characters including these spaces has been retrieved again call the deed method.
The argument forward.
Now let us not pass any argument to the deed method execute this line of code.
So if we do not pass any argument to the deed method it reads all of the remaining text in the file
from that the cursor is position.
So this particular state Moon has a red beginning from where the court said his position to the end
of the file if dot read.
Now again if you try to read the file again it returns an empty string because the cursor does now at
the end of the file.
Now you can use the seek method FDR to seek to bring the cursor back to the 0 8 position so we can use
this method to bring the closer to the specified position so I past 0.
Now let's read the file again by passing the argument forward.
It's gonna be the first four characters.
Now if you want to know the position of the cursor you can use the tail method on the file and it shows
that it is a D fourth position similar to the deed method.
We have the read line method which added individual lines of a file.
Let's call the read line method on the file object so it has read the first line from there D.
Because it position to the end of the line that is the first line let's call the read line method again.
So the entire second line is read so the second line is read from the initial to the ending position
next.
The next method is d e.g. lines method and this method returns a list of remaining lines of the entire
file.
So this is going to do it on a list of lines.
So this is the third line which is one item and this is the fourth line which is the second item in
the list.
And then you can use a for loop to read the file line by line.
And here is the code for reading each line from the file.
The for loop is going to retrieve each line from the fine print the line.
And finally after the for loop breaches of the file we are going to close the file using the close method.
So let's execute this piece of code.
This is how you can use a for loop in order to access each line in a file.