So in this session we'll learn about file handling and we'll see how you can
read and write to your files using Python
So coming to our last topic of file handling in Python. Well file handling is
a very important of any web application. Well you have four different features
which you can perform in a file handling. You can open, read, write and delete a
file using Python alright. Let's see them one by one. The first one being open
so you can use open function to open a file and it takes two parameters the
file name and the mode. Let's see the syntax. Syntax is f equal open path of
the file so you have the syntax as F equal open path of the file and its mode
fine. Let's see what are the modes
so we have four different types of mode: read, append, write and create. For reading
you specify "r" it's a default value and it opens the file for reading and if the
file does not exist it will return you an error
next is the append and the mode is "a" alright what it will do, it will open a
file for appending and in case of the file does not exist it would create a
new one next mode is "w" which is write. It opens a
file for writing and creates a file if it does not exist fine. I'll tell you
about the difference of appending and writing later in this section
alright. Next and the final - create, that is "X". It creates the specified file and
it returns an error if the file with the same name exist
it creates the specified file and it returns an error if you already have a
file with same name fine. Let me just show you
like F equal open, path of the file
I will open it in my D Drive, D slash name of the file, test file
dot txt and comma
what should be my mode it does a reading mode. I hit enter
okay
so as I told you earlier it would throw you an error if the file does not exist
right if you want to create it you have to mention X up here
and it will create your file let's go to our D directory and let me just show you
the file D, so you can see that test file up here
right so this is how you can open a file in Python or you can even write "w" up
here
it won't throw you an error
since it creates a file if it does not exist but it won't throw an error in
case the file with same name exists
next is the read function
well suppose this is our file. It consists on document hello welcome to
demo file dot txt, this file is for testing purpose good luck. Let's open our
demo file, test file, hello everyone
welcome
to Intellipaat, good luck
save it
save it as test file on one fine. Let's see what a operation we can
perform on it. Now what if I want to read it? So how will I do it? So f equal open demo
file dot txt comma mode R. R is what? reading mode and then print F dot read
fine? Let me just open that file for you F equal open the path of the file
it was D colon slash test
file1 dot txt and in what mode I want to open it in read mode
and I want to print all the content which is present in that particular text
file so f dot read it first and then print it hit enter. So these are the
content of my file hello everyone welcome to Intellipaat and good luck
fine? Now what if I want to read some parts of
the file? Like I want to read the first five
characters of the file so what will I do let's see just copy and paste it
I want to read the first five characters so just mention five up here. What
will it do? Print first five
characters from the
text file. Hit enter So you got the output as hello zero one two three four
and five so these are the first five character from the string alright
so now what if you want to read the first line so for that you have a
function as read line all right yeah function as read line which would
be used to return one line or the first line again copy it
we stood up here read read line just hit enter sorry you got
an error okay I need like okay sorry L is small
read line so you got the output as hello everyone
this is my first line
next what if I need to read first two lines of my document so what will I do in
that case? So in that case what you can do you can use read line twice and it
will print first two line of your text like this again do it once more
f dot readline, hit enter in the same manner okay so what you got
so you got the first line from your text all right
now looping through the file. Now what if you want to read the file line-by-line
so you can use a for loop in that case
so it's like for "X" in F
such that
print X. If X is 1 then print first line, X is 2 then print second line, 3 so on
all right so you'll be able to read the file line-by-line. Hello everyone welcome
to Intellipaat and good luck
next is reading and creating. Now to write to an existing file you must add a
parameter to the open function like you can write "a" or "W", "a" will append the file
that it will append or add something at the end of the file and "W" it will
over write any other existing content for example
let's go to be straight up here instead of our let's mention a it's for our
bending and
f dot right whatever you want to write
this is the appending statement
executed
thing now if you will check the file let's check the existing file
wenneck and had read line mode are for X and F
print X executed so yeah so as you can see this is the appending statement got
added at the end of the file now what if I use
w instead of a
so what will it do in this film
overwrite my statements
so here I can write print f dot right
print F dot right this will
overwrite everything
executed done now let's check it again what are
the contents of my file
so if you check it so only this is left this will overwrite
everything fine so this was about are you can write to an existing file
Ali
next we have is creating a new file well you can create it using X a or W using X
creates a file and it returns an error if the file exists append it creates a
file at the specified file does not exist
and finally W it creates the file in the specified file does not exist
for example F opened my file or dxdx it will just create a new file
in case you are attempting to create a file with the same name or to throw you
an error for example
don't move this
this file already exists right if I am trying to create it again
it will throw me an error
all right as file already exists or file exists fine
next and last we have is the Delete put OS Mobile is used to delete a file
how you can delete it import the OS module or start remove demo file dot txt
that's it but for that would you have to do you
have to first close the open filing so f dot flows inside this mention the name
of the file at the path of the file d : /
test file one dot txt first close it
sorry
you
like F dot close
close the file now if you want to delete it you need to import OS mode you'll do
it in photo s and os dot
remove
that name of the file what is the name of the file
test file 1 dot txt that's a
you
got the error the system cannot find this specified file so I guess the file
is now start deleted
okay I haven't specified the path of the file I just written the name of it so
I'll write the colon slash and the file got deleted so yeah there is no test
file one alley fine
so thank you guys this was all for this session in case you have any doubt feel
free to add them on the comment section below we'll be more than happy to
resolve your queries thank you
so this was all for this session in case you have any doubt free feel to reach
out and telepath 24/7 and we'll try to resolve your query at the earliest thank
you