filmov
tv
38 How to write content of a file and create a text file in Python?
Показать описание
Hey guys,
welcome to Free Tutorial TV and Python tutorial for beginner.
In this video I'm going to show you how you can create a text file in Python and write in it.
So let's get started.
So do start with we use a built in function which is open and it takes two arguments.
First is the name of the file which we want to create or read.
So let's say I want to just create a file which is then open demo dot T X T in the same folder in which
I'm working in.
So you can see I'm working in my file handling project and there is only one file there which is test
dot P Y and this demo dot the TXT file will be created in the same folder.
Now the second argument which we want to give here is the mode.
So there are several types of modes which we can use with this open function to work with the files.
So let me show you the list of some of these modes so you can see on the left hand side I have listed
them modes.
So you can use the R R R plus or w r w plus or A or B.
Now R is used to read from the file.
And if you don't provide any mode to this function then the default mode is that read the mode.
Now if you want to write to a file you use this write mode with a keyword w.
So we are going to use this w keyword in order to write to the file.
So let's use this w keyword here and then this open function returns a file object.
Select store this file object into a variable.
Now generally we call this file object our file handler. and using this file handler we can work with
the file on which we are working on.
So this file handler will be used to work on this file. which we want to create. in the next line.
I will just use this file handler and then I can call some method here.
You can see that is a list of method. I can call on this file handler which is written on the by this
open function.
So to write into a file I can use the write function and here inside the write function you can provide
any text whatever you want to write to the file.
So let's say I just want to write this line of string to my text file. which I'm creating so I can just
paste the text here. and then after writing to a file when you're finished working with the file you
use Once again this F H and then call a method to call close on it.
Now what does this close function do.
So this close function is used to close the file and immediately free up any system resources used
by your file handler. which is F H and it's a good practice to use this when ever you are done with
the handling of your file.
So always remember to use this close function after you are done with the handling of your file.
So this is a good practice but I would say you must call this function.
And that it, So this w mod if the file demo dot text doesn't exist . it's going to create the file
and then it's going to write this text into the file so that W mode is responsible for creating the
file also.
And if the file already exists it will override the previous string by the string which you are writing.
Using this write function.
So let me just run this code and you will keep an eye on this project because this file will be created
inside this project. so I'm going to right click and then run this test file and you will see as soon
as I run this test file . there is one more file created here it is Demo dot TXT.
And when I click on this file you can see the same text appears here. which we have written using the
write function.
**************************************************************************
please like and follow us in Facebook and Instagram.
welcome to Free Tutorial TV and Python tutorial for beginner.
In this video I'm going to show you how you can create a text file in Python and write in it.
So let's get started.
So do start with we use a built in function which is open and it takes two arguments.
First is the name of the file which we want to create or read.
So let's say I want to just create a file which is then open demo dot T X T in the same folder in which
I'm working in.
So you can see I'm working in my file handling project and there is only one file there which is test
dot P Y and this demo dot the TXT file will be created in the same folder.
Now the second argument which we want to give here is the mode.
So there are several types of modes which we can use with this open function to work with the files.
So let me show you the list of some of these modes so you can see on the left hand side I have listed
them modes.
So you can use the R R R plus or w r w plus or A or B.
Now R is used to read from the file.
And if you don't provide any mode to this function then the default mode is that read the mode.
Now if you want to write to a file you use this write mode with a keyword w.
So we are going to use this w keyword in order to write to the file.
So let's use this w keyword here and then this open function returns a file object.
Select store this file object into a variable.
Now generally we call this file object our file handler. and using this file handler we can work with
the file on which we are working on.
So this file handler will be used to work on this file. which we want to create. in the next line.
I will just use this file handler and then I can call some method here.
You can see that is a list of method. I can call on this file handler which is written on the by this
open function.
So to write into a file I can use the write function and here inside the write function you can provide
any text whatever you want to write to the file.
So let's say I just want to write this line of string to my text file. which I'm creating so I can just
paste the text here. and then after writing to a file when you're finished working with the file you
use Once again this F H and then call a method to call close on it.
Now what does this close function do.
So this close function is used to close the file and immediately free up any system resources used
by your file handler. which is F H and it's a good practice to use this when ever you are done with
the handling of your file.
So always remember to use this close function after you are done with the handling of your file.
So this is a good practice but I would say you must call this function.
And that it, So this w mod if the file demo dot text doesn't exist . it's going to create the file
and then it's going to write this text into the file so that W mode is responsible for creating the
file also.
And if the file already exists it will override the previous string by the string which you are writing.
Using this write function.
So let me just run this code and you will keep an eye on this project because this file will be created
inside this project. so I'm going to right click and then run this test file and you will see as soon
as I run this test file . there is one more file created here it is Demo dot TXT.
And when I click on this file you can see the same text appears here. which we have written using the
write function.
**************************************************************************
please like and follow us in Facebook and Instagram.