Full transcript of “File Handling in Python | Python Tutorials for Beginners #lec95”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 hey everyone I hope you are safe and doing good so in the series of learning Python programming language we have discussed inheritance Concept in python in the previous video we have seen diamond shape problem in Python right and how to solve that problem in Python right now the next topic is file handling in Python so this thing we will see in this video and basically what I'm going to cover first why why you need file handling because till now we have done many thing in you know python we are we have written many programs we have done many projects and why the need of this New Concept right even if we are able to make project in Python then why this file handling concept so this thing is very important so first whenever you if you're a teacher so whenever you want to teach a New Concept if you are a student
0:45 you want to learn A New Concept the first thing you need to clear is what why you need this concept right so first thing we'll see in this video why even if we need files why we need file handling concept second thing what now what is file handling what are files write this thing then how how we are going to implement file handling in part so these three things we'll see in this video right so almost everything about file handling so this video is going to be a little bit lengthy right it's like you can say file handling in one video that kind of video right okay now why why you need file handling see whenever you run a program then what is the process see we have we have Ram
1:32 and we have disk hard disk this is a permanent memory known volatile right so when you are whenever you want to run a program then that program would be first loaded into main memory right so suppose let's take simple example addition of two number so this is our console and we will take two number and like one and two and addition is three so addition of two number so that program would be first loaded when you want to execute that program into main memory right so the values whatever value you will take from console and the output that is three that is everything is in main memory right so we take input from console we just print something the output on console using variables right
2:19 till now we have seen that thing and once you close that application once you close this program everything is gone whatever data you have worked on you have worked upon in this program addition of two number that is gone now right because that was a non-volatile memory RAM memory right now what if you want to store this data permanently okay that is just a simple example why you need to store like these two numbers and addition of these two numbers that is just a simple example I just want to just I just want to clear the logic if in some situation you want to store the data on which data you have worked previously in that program or the output of the previous program the previous you
3:04 know uh whenever you run that program previously if you want to store that thing permanently so where you need to store obviously in disk hard disk so here Files come into picture using files we can store that data permanently on disk and here the file handling concept comes into picture second reason is if you want to work upon a large amount of data suppose if you want to input a large amount of data so obviously that is not possible with the help of console from console like you just write down the data you just give input that amount of data using console that would be a tedious task so what we can do suppose that data is stored in a file so we can directly fetch the file
3:51 from the hard disk so that pile would act as input right this file is having a large amount of data so we can directly fetch this file and that would be as input inner program rather than just typing that amount of data third thing obviously Ram is limited amount of memory right so if you want to display a large amount of data that is also sometime not possible with help of console so we store that data in a file directly right on hard disk and we can view that file right if you want to display large amount of data data so that is also not possible on Console because of limited space limited RAM memory so we can store that data into
4:36 the into disk with the help of files so these are some reasons we need files we need file handling right so file is simply what it's just a sequence of bytes just sequence of bytes where you will store data permanently on secondary map your own hard disk or you can say file is simply a container or we can say file is simply a named memory location where you want to store the data permanently using that you want to store data permanently on disk simple and managing of these files is known as file handling
5:21 like open this file read data from this file write data into that file closing a file right these kind of operations we will do so that thing to manage the files to do these operations that thing is known as file hanging so now I hope you got the reason chance of this question why file handling concept right I have told you mainly three reasons also we have seen what is a file and what is file handling now in file handling okay before that types of file basically two types of file text file and binary file this file as the name suggests it will store data in human readable form means it is a sequence of like alphabets
6:07 characters numbers those are text files we can directly open these file into with the help of text editor and we can read whatever is there in that file like the extension is dot txt file dot CSV file like dot py file these are text files place now binary files so binary files contains known human readable characters and symbol we cannot read those file directly we cannot open these file in text editor right those are binary file and these are used to store images audios videos zip files compressed files and executable files these are binary files okay these are having a special type of
6:54 coding which we cannot directly read which a human cannot directly read like in text files right so these are two types of file text and binary on both the files we are going to work in this video with the help of program I'll show you right how to open a text file binary file how to read a text file how to read a binary file how to write a text file how to write a binary file with the help of no image will do this thing right okay now first thing if you want to do basically some operations on files are open read write and close these are mainly four operations but there are many more operations as well like tell operation C corporation many operations
7:39 so mainly these four operations right so first of all you need to open a file right if you do any other operation like if you want to read and something from that file first thing obviously you need to open that file suppose like this I have this file like I have this file notes file so if you want to read something from this file first I have to open this file right same thing we need to do in case of program if you want to write something then also first of all you have to open this file then I can write right and once Reading Writing on or any you know operation I want to perform are done then I'll close this file so this closing is also very important because whenever you open the file and read and write then some memory
8:27 would be allocated to these five objects and all so you have to close that pilot class to free that memory so that that memory can be used by some other process right okay now how to open the file first syntax is what to use this open function this is inbuilt no need to import anything right and mainly two parameters you need to give first is the file name right and then next is the more in which mode you want to open this file and this open function will return you can say a file Handler or you can say an object so that we will store in a variable suppose file object this is
9:13 going to return object so if you want to do any operation on this file Reading Writing or anything or closing the file with the help of this file object we will do or file Handler this means file Handler or maybe file pointer if this is a file so this is basically pointing to this file it's just a pointer to this file like if I want to read the file then from where I want to read that file so this pointer will show me from where I can read the file if you want to write the file write something in the file then this pointer will show like from at which position I can write the file like this so this file Handler or the file pointer is very important so whenever you open
10:00 this file this open function will return that pointer for that if you read write then we will do all the things with this this pointer over this file object or you can say with this file Handler right now what are these modes the excess modes means in which mode you want to open this file what type of operation you want to perform on that file that could be decided according to the mode according to that only you will open that file in that particular mode so basically six type of mode read write append right and then R plus W plus a plus don't worry I'll discuss this thing practically with program right
10:46 so R means it will open the file in read mode write means in right mode means if you want to write something in that file a means append mode append means see right or append are different right append means you want to add something at the end of the file if there is some text in the file suppose I have a file and here I am having hi so append means at the last of the file I want to add something this is appending here means after this text from here I want to add something R Plus for both read and write W plus is also for read and write a plus is also for like read or you can say a read and append but obviously what is different
11:33 between R plus and WS that would be cleared when we take one example but I hope the theory is clear to you guys right now let's see these different modes with the help of program okay so let's first create a new file let's name it file handling Dot py okay so first thing obviously you need to open a file if you want to read or write something in that file so how to open I have told you a syntaxi I have we have this function this is built-in function so no need to import anything open and what you need to pass the file name and mode and obviously some extra parameter also but right now the main two things you need to focus is file name and mode you need to pass so
12:19 what file name I'll pass because I don't have any file right now so if you want to first of all create a file for that also we have something we have X mode or we have right mode as well that also will create a file but let's discuss first X more so just name a file suppose file one I want to create this file and Dot txt and after that comma mode I want to give X in single code to double quote you can give this thing right and then whatever this open will return at that file object or the file Handler that I'm going to store in suppose F1
13:05 4 5 1 f 1 5 pointer or file Handler okay let's run this and see what will happen let's run this see nothing would be shown so the main mode of this x is to create a file name of the file is file one so let's say this file has been created or not see here in this we have this file 1.txt and if you open this file we have nothing in this file right so X is used to create a file but the problem with X is what if you want to create again this file the same file with the same name then it will give error so the same program file 1.txt if I want to run again let's run this
13:51 you will get an error see file exist error file exist this file one already exists so you cannot create a new file with the same file name so this is the problem with this X so if you are sure that this file doesn't exist here then you can use x but if you are not sure that this file exists or not you can use right mode how to use write that also I'll show you okay now basically we have created this file file one row txt but there is nothing in this file right so if suppose I write something in this file like hi so this is I have written like hi I am Jenny and we are discussing file handling in Python 2 line I have written in this file so now I want to read the file so in which mode you need to open the
14:38 file name and mode you should give r so see what is the SAR mode different types of mode are more this R mode will open the file in Grid mode you cannot write something and the file Handler or the file pointer exist at the beginning of the file at the beginning of the file next point is it raises it will give an input output error if the file does not exist right and it is also the by default mode for opening the file so this is our mode okay now let's see this is this has opened this file it will not print anything see it will not print anything but this has opened the file open means this F1 is now a five pointer profile object that is pointing at the beginning
15:25 of this file at the beginning means here high at H at H it is pointing at H now the file pointer so if you want to read the content then we have a function read so how you can use that function F1 Dot read and whatever you will read the content suppose I'm going to store in a variable data and then I am going to print this data right let's run this see hi I'm Jenny and we are discussing file Landing in Python so whatever the content was in that file that was on our console now so we have fetched the data from that file right this is how you can read the file but this is by default also so
16:10 if you do not write down this excess mode and if you simply write this file name then by default the mode would be considered as read mode so if I run this then also the same output you will get right but if this file does not exist suppose I am writing R here and if this I am giving a file underscore the name underscore 2 so we know this file does not exist here right so it will give error let's run this see file not found error in line number one in line number one here file to row text no such file or directory so if file don't exist it will give a array right so this is what read
16:57 mode next is you cannot write you cannot perform the right operation in this mode right if you want to write something in that in this file or in any file then we open the file into W mode that is right name okay now let's first read what is Right mode c r plus we will see later right mode this will open the file in right mode only right we cannot read the data from the file if you open the file in write mode it overwrite the file if file already exists if file already exists then it will override the content of the filament it means it will erase the content of the file and then whatever you will write that it will
17:43 write the new content it will write in that file or it will create a new file if file doesn't exist right and where is the position of file Handler the file Handler exists at the beginning so this is what right mode this file to does not exist let's just comment out all this we know this file 2 does not exist and we are opening this in right mode so it does not exist it will create a new file let's run this obviously do not print anything but now we have this file too a new file there is nothing in this file right so if file does not exist you will not give any error it will create a new file right but if file already exists suppose I want to open file one file one
18:28 already exists right and see in file one I am having this content so this will be erased right this will be erased when you open the file into write more the previous content would be erased and new content whatever you will write that would be added so let's see open this file one right now I have this thing in right mode obviously it will not show anything but let's see now file one see now there is nothing in file one now I want to add something I want to write something so just call
19:14 write function on the file Handler F1 dot right what you want to write let's write down something like this I want right welcome to Jenny's lectures so F1 dot right the file content is at the beginning so from the beginning it will write so welcome to Jenny's lectures right let's run this obviously if you are not reading anything from the file so it will not show anything but whenever you see this file now file 1.txt see welcome to Jenny's lectures this is written welcome to Jenny's lecture and previous content was erased right okay so this is what right if you want
20:01 to access suppose if you want to access like let's read the content whatever this in this file so simply print and call this read function it will give error see unsupported operation not readable means we are opening this file into right mode so it will open the file in right mode only read operation would not be supported right so this is right mode now open the file into R plus mode R plus means it will open the file in both read and write mode right but okay let's first I'll show you see this is R plus more open the file in both read and write mode and what is where is the file Handler file Handler
20:47 exists at the beginning plus if I don't take doesn't exist it will give error it will not create a new file right so this is R plus more you can just note down this thing okay let's do this R Plus so if file don't exist suppose I want to exist I want to open file 3. so obviously it will give error let's just comment down these line first and let's run this so if I don't doesn't exist R plus will give error no such pylo directory exists file 3.txt in line number one where we are opening this one right so let's open the file file one file1.txt in our plus mode both read and
21:35 write the file Handler or the cursor would be at the beginning right now it will open like it will not give any error see obviously it is not printing anything but it is not giving any error right because file one already exists welcome to Jenny's lectures with the content in the file so R plus is not going to erase the previous content unlike W mode okay now first why we need this R plus mode basically if first you need to read then write in R plus mode first the processor should be read and write so read let's read the content of this file
22:23 just print F1 dot read and after that I want to write something so let's fry down here this is python course right I want to write this thing so both read and write we are doing the same right let's run this so first we are reading so welcome to Jenny's lectures because in file one we are having welcome to Jenny's lectures right then we are writing this is python course so see in the file now we are having this is Jenny's lectures and this is python course it is only printing welcome to Jenny's lectures and after that it is adding
23:09 here this is python pose okay now the thing is first I want to write let's write first what will happen so because obviously both read and write we can do so I want to write first then I I want to read okay let's see F1 Dot right and just I want to write hi that's it and then I want to print F1 dot whatever the content in the file what do you think what it will print pause the video and think about this let's run this see it is what it is printing lcom two
23:56 Jenny's lectures this is python course why so because first it will open the file in R plus mode so at first the cursor would be at the beginning previously file was w-e-l-c-o-m-e welcome this was the file welcome right so cursor was at the beginning okay now because in R plus more when you open the cursor would be at the beginning now I want to write h i high so cursor is at the beginning so it will write H here I here it will h i so w would be erased h i so if you want to modify the content then open this file open the file in Applause
24:45 is here now from here I want to read next is read F1 dot read so F1 is at this position now and so it will read from this position only so it will print this line only not h i I hope you got the working of this R plus more that is what I was saying first read the content if you want to read what is in the file the first read then write because first if you will write then it will modify the previous content hey so wherever if you want to modify this file like a large file and two or three line I want to modify right then just move the file pointer to the particular position and then you can just override that content only if you want to do this
25:32 kind of thing then you open the file in applus mode how you can move the cursor we have a seek operation right so if you want to find out where is the file pointer the position then we have a we have a function 10 so F1 Dot l after printing after writing also I want to print this if one the position here and after reading as well I want to find out the position of the file pointer 3 time so let's run this let's just do again welcome to Jenny's lectures and this is
26:18 python code this is our file now I want to write down high and I want to read then the position of the cursor so at first see the file pointer was at zero position at first after opening right once you have written high now it is at second position right 0 at zero position then one and add two position at two here was the cursor then after reading this content after reading after that the file pointer position is at 48 means in the last here after reading this thing at last it will be at 48th position right so this is how you can just if you
27:06 want to find out the position of the cursor or the file pointer you can just use this method so this is R plus mode and first if you read and then if you write then first if you read then you read from here and the cursor would be at last and then if you write then it would be something like append because we are writing at the end of the file so it's like appending in the file okay so this is R plus mode now we have W plus mode so what is difference between R plus and W plus let's see first W plus see this is W plus mode it will open the file in both read and write okay same as R plus but and as well as the file Handler exists at the beginning of the file okay same
27:51 it overwrite the previous content if the file already exists then it will overwrite the previous content right and if I don't think doesn't exist it will create a new file right so in overwritten in this concept it is different from R plus more R plus more if I don't exist it will create it will show an error but if W plus mode if I don't doesn't exist it will create a new file and it will override the previous content if file exists right let's see so we are opening this file file one in now in W plus mode so if file don't exist suppose file 4. this file don't exist let's just comment
28:37 out these things so it will create a new file file 4. no output but we have a file 4 right and yeah at last better to use first close F1 Dot close this thing is important right okay now already existed file I want to open so file one so right now in file one I have this thing suppose I have welcome to Jenny's lectures and this is python course so if you open this file in wplus mode it will open in both ingredient right what but previous content would be erased so now in this file see I have this content once I run the score and after that I'll show you the file one
29:23 then there would be nothing see just run this nothing will be shown here but if I want to check file one see there is nothing in this file previous was erased means it was overwritten now now obviously if you want to read anything there is nothing in the file so first thing is what should be the process first write then read and in R plus it was first read then write that was the you can say exact or you can see the correct process of reading and writing right now first write something in this file so how to write F1 Dot we have an operation right and I just want to write here hi and welcome that's it
30:09 right now I want to read so and see two time I want to write F1 dot write High welcome F1 dot right this is python course and then data then I'm reading the data okay because obviously both we can do read and write let's run this see it is not printing any output so this is you know where the student most of the student get confused we are reading the data and obviously W classes for both reading and writing but still it is not showing anything what is the reason this point is very important see okay let's see this file one do we have any data yes hi welcome this is python
30:55 course so see high welcome and this is python course but it is not printing anything why so the problem is see let's first check out the position of this thing position of the file pointer using F10 so first after opening the file check the position then writing High welcome check the position right then after writing this again we are going to check the position then after reading the data or after printing the data check the position right let's just delete these
31:41 things so this is our program basically right fourth time we are checking the position let's run this see only position it is printing no data first it was at zeroth position at first obviously the five pointer was at in the beginning when you open this file means here in the beginning right now we are writing High welcome so at first there was nothing in that file then High welcome was added High welcome was written in the file and after writing this the position of the file is at 10 10 number 10 position so one two
32:28 three four five six seven eight nine ten after e which is from zeroth if the position would be started so here was the file pointer After High welcome then we have written this is python course after writing this again we are printing then after writing this the position of the file pointer was at 31 31 is here at the end right now we are reading the data but position of the file pointer is here and after this there is nothing in the file that is why it is not showing anything what it will read so to read the data you have to move the position the file pointer the file Handler again at the beginning
33:14 then you can read so for that also we have a function so after writing this just here I am moving the that file pointer we have a function seek and add 0. obviously you have to pass a offset and from where you want to move two types of two arguments basically but that also will see later when we discuss seek function in detail right now just pass zero means at the beginning I want to move right now it will show see hi welcome this is python course and the position of the you can say
34:02 file Handler now after moving this cursor again I want to print where is my file pointer where is my file Handler let's run this see after moving after here once it was at 31st it was again now at zeroth position at zeroth position because we have used this seek operation seek function right now it is printing everything so this is how you can read in W plus mode right I hope now you got the answer of your problem right and this is very common problem students see because this is both for reading and writing we are writing we are reading but it is not showing anything on the console why it is not
34:48 showing I have told you the reason okay this was W plus next is append mode a is comment out this thing and we will again open this F1 Dot sorry F1 equal to open and file name is now first let me just show you what is append mode see it open the file in append or you can say right mode but obviously write an append a difference right means it will erase the previous content the cursor would be at the beginning or anywhere you can write using the C corporation we can move the file pointer in any way you can write append means by default it is going to append means at
35:35 the end of the file right so the file Handler exists at the end of the file the previously written file here the difference and if file already exists then at the end of the cursor would be at the end of the file the file Handler if doesn't exist it will create a new file right and the newly written data will be added at the end of the file this is a pen mode so file one file one is we are having data in file one hi welcome this is python course right if there is no such file suppose I just want I just have deleted this file to as well as file four so there is only file one so let's open file two there is no such
36:21 file in file two let's run this it will not give any error and see now we have file 2. this is a file file right so if your file is not there then you can create a new file but if file is already there like file one okay now just open this inner pen mode and let's write down something F1 dot right and I just want to write like hello students okay let's run this see it will not show anything but in file one I have now hi welcome this is python course and hello students append append means at the end because the file pointer was at the end and from here it will add hello students right but it will only open in the append mode
37:07 on read mode if you want to sorry write mode if you want to read then we'll give error unsupported operation it's not readable in append mode you can only append you can only write at the end of the file if I don't take this it will create a new file right okay now let's do this a plus node let's open this in a plus mode a plus means both append and re right now first if if you read if I do not append anything if I don't write anything and because I have told you file one already exists in five align I have like hello students so let's just remove hello
37:52 students and this is our hi welcome this is python code this is our file OneNote txt right I want to read this because I've told you in append mode it will open the file both in read and append mode so maybe you think let's read what is the content then I will append it right okay let's read F1 dot read it will not show anything see this is again a problem there is no error but it is not showing anything and why so and yeah in file when we have content hi welcome this is python course see a plus mode is what when you open this file in a plus model in a mode append mode the cursor or the file pointer is at the end of the file means
38:38 at the end means here and from here you want to read so obviously after this there is nothing in this file so what it will show nothing if you want to check after opening let's just print the where is that position of that that file pointer see at 33rd position means here and the last so how you can read anything right what is a plus more it will open the file both in append and read mode append or you can say write and read more file Handler exists at the end of the file if file already exist or it will create a new file if file doesn't exist right
39:25 but it will not erase the previous content unlike W plus mode W plus series is the previous content or you can say it will overwritten the previous con overwrite the previous content so directly if you want to read just move this cursor at zeroth position now it will work fine see first at all the 33 but we have used the C corporation and then High welcome in this python force that is fine right now if you want to you can say write something so let's write F1 Dot right Jenny's lectures right let's run this okay this is printing this thing but in
40:10 file we are having hi welcome this is Jenny's lectures at the end because we are appending cursor was here right so I hope you got all the six type of modes but now if I have a binary file suppose I am having a text sorry image let's have an image so this is an my image one dot jpg this is in this project in this project only right if your file is not in this project here in this directory where you are working your python directory if it is supposing desktop that you have to provide the complete path here like this a file name is
40:55 my file underscore desk dot txt this file I have on desktop so directly I cannot use my file underscore decks.txt see let's just comment out this line see I want to open this my file underscore Tech desk dot txt in read mode this file is on my desktop so let's run this and it will give an error see in line number 16 you are opening this file and no such file or directory file not found right so if you want to open this thing then you have to give the complete path like this like this the complete path now we can open this file in any mode if
41:42 you want to open like in read append or anything let's run this C and in that file I was having only High I at first and then I avoided Jenny's lectures I was previous content in this file and now I have added Jenny's lectures I was previous content and then I have added I avoid Jenny's lectures right so this is why you have to give the complete path if you want to access the file which is outside of this directory right now let's have an image I am having my like this image my image one dot jpg this is my image right this is my image file in this project only in this path only right now if I want to read this thing so let's just
42:31 my image one load jpg and let's just read this nothing else I want to do okay in read mode and then I want to read the content of this image what do you think what it will print let's see safe okay it is giving this error Unicode decoder okay because that the mode is for binary file it is RB R was only for text file RB WB R plus RB plus WB plus these were was for this is for this is the access mode to open binary files right so RB let's run this
43:19 see what you are getting here this is something we are not getting anything because this is binary file we are not able to read what is this so if I want to move this file this image into another image file only I want to show that thing so I can take like F1 is equal to open and my image 2. sorry jpg and that I'm going to store in WB mode means right mode I want to write this image into here right
44:04 so just use for Loop for I in F1 for in F1 right just write in sorry let's take obviously you have to take second pointer F2 so F2 dot right and whatever is this in this I so whatever we are getting from F1 that data we are just writing that into F2 okay so we are creating another a second image let's comment out this thing and let's run this obviously it will not show anything but it has created a image to file C my image to node jpg if you open this now
44:51 you are getting the same image that was in my image one my image one my image two same image so like this you can also work on binary files right this thing you can try but the mode is different rbw will be like this okay this thing also you can try so this says what all about is file handling the modes and T still operation till and see if operation and all the different type of operation and all the type of modes I hope you heard this right so just practice this thing with different different examples so I hope you got the file handling Concept in Python so now I'll see you in the next video till then bye take care