Tip:
Highlight text to annotate it
X
Hi guys this is Tamoghna and welcome back to my Python tutorial. In this tutorial
I will be discussing about files. Now working with a file is just like working
with the book. Suppose if you want to read or write something then first you'll have to
open the book, do the necessary operation and then close the book. So similarly
when it comes to file, you will have to first open it, you can either read or write whatever you want and then close the file. So in order to work with file, you'll have to
declare a file object. So suppose I am considering f as my file object followed by inbuilt function open. Now in order to declare file
you have to first create a folder just like I have created FILE folder within my CODES folder
where all the codes are kept. So you can see here, there is nothing present in the
folder FILE.
So now in order to create text file, I have to use this path. So copying this path, then I have to paste it, followed by name of the file, suppose cortana dot txt
then comma, followed by the mode it can be either w or r, that is write or read. So now it has been set in
the form of
write mode. Then f.write
Whatever you want to write within that file. I am writing Hi
I'm cortana. So then you'll have to close the file, and if you go now, you can see here that cortana has been created within the folder FILE. So, Hi I'm Cortana
has been written. Now if you want to read what is content present in a particular file,
then you just have to write this entire portion that is f = open followed by the
path, then the file name and in this case, instead of w, you'll have to write r.