Hello everyone, welcome to Magnet Brains. Let's start our chapter two Complete revision. Ok? Hum Ye One Shot I will complete it. Ok? So first see what topics are we going to study Are? So first is introduction to files what are those files? Why do we need this Do you need it? Ok? Next is Types of Files. How many types of files are there? So There are only two types of our text files end binary file. Ok? Next is Opening And closing a text file. Now in this we Generally speaking, those who will do more work and use Python We will perform operations on whom? Text files. Ok? In Python, we We have a text file, we can use it Will learn to open and close. opening a file, closing a file and we open the file We will do it with a with clause. we have one The clause is with it through. Ok? Now The next is writing to a text file. Ok? Now we open a file, I closed it. what can we do in the file Are? You can write it down as well, you can also write it down Are. So for this we use the right method and the right Lines Method We will look at both these methods. Correct Is? Now the next is reading from a Text file. Now we can use any text file What can I do with it? You can also read it. Right? So for this we have some methods There are. read method, read line method end Read lines method. So through these three methods Will see that too. We are theoretically together We will see it practically also. Ok? Now, the Next is setting offsets in a file. Now What is offset? this is what we are saying in this topic I will understand. First of all, in any file What is offset and how to set it I am in a particular position. Ok? Its Also we have two methods. tell method and a Seek method. Right? we will look at both of them are going to. Now the next is creating and Traversing a text file. Now we have a The file has been created. It means our If you have the file then it is fine. Now the one whom we open, Close, read, write, they can do it all. What can we do now? create the file You can do it also. Ok? Now the file it already exists so it is open and you can close it but if we get any If you want to create a file then you can do that as well Are. And traversing a text file. Now What is traversing? like for For example this is a file and there is something in it The characters are written. Right? So every one Go to character. Ok? what do we tell him Do you say anything? Traversing. so how in the file Do you traverse? whatever is ours we How to print stored data Can you? They will all see it in this. Ok? Creating and Traversing. Next Pickle Module. Now we have a module Pickle module. These are our binary files. whatever we have been reading before till now something read, write, open, close, whatever So we read this offset, setting, create, Traversing this in which will we do it? Text In the file. Ok? Take on the Pickle module. What do we do in pickle module? Pickle In modules, we deal with binary files. Are. Ok? so how in binary files data is stored, retrieved, There are some methods for that. Dump Method End They will look at the load method. And the last is file Handling using pickle module. figure We can do file handling using modules do. Ok? So let's start again Are. are you ok now? So first is introduction to Files. in real life we often need to save Data permanently like employee records, Sales, Details, Product Inventory. If We don't save data, we will have to type it again every time which is a lot of Extra work. What does this mean? that which What do we do in real life? We have some data which We need to store it permanently Is. Like for example if you are a businessman Yes, if you have your own company then you What needs to be done? Whichever employees you have what to do with their data that works here Does matter? Has to be stored. You Are you a salesman or do you deal with sales? If yes, sales data or product Inventory all this data to you permanently Has to be stored. But if you this What if you don't store it permanently? will be? What do you have to do? Go to the Daily That same data will have to be stored again and again. It will be removed and then it will have to be stored. which is A Lot of Work and Practically Not Possible. that you can store the same type of data You are writing again and again. Right? and that What is it practically? It is not possibile. Correct Is? And there is a lot of extra work. So What is its solution? Use a file. Correct Is? What should you do? create a file and What should you do with all the data that is in it? Store it. Ok? so this is the best one Whose option is ours? Use a file. Next. Now what is a file? Ok? this has become a problem that What is the solution? File. It's fine now? Now What is a sol file? a file is a Named place on your computer, secondary Storage like hard disk and pendrive Where you can store data permanently. Correct Is? What is a file? A named place. a name is there any place for. Ok? any name be it Can. There's a place by any name where What is data? Ok? Our computer a sorry our computer There is no such thing in secondary storage What is the place where we store data? Store it permanently. Ok? You You can understand it exactly this way. Python There are variables like in . Variables what happen? There is a name. This in our computer system in a way Secondary storage has a name something like Too. Ok? The name can be anything. Where But what do we do? Permanently store data Let's store it. Ok? We are on our side You can give anything from it. for example if We are storing student data So we will give the name of the file here. Students data. Ok? I hope it is Clear to you. In Python this is normal We talked about the file. you are right in python programs and save them with it dot py Extension. This is also a file. when you You write code in Python, you write programs If yes, then what is that? The file itself is what you can find in py You are storing with extension. Right? They what is it too? The file is there. Because you are your Store data, your code, your program had been. You can store input and output data in a File two. So you can use it later. Ok? What can you do? You can store What are your input and output data into? File Me too so what do you do? Later you You can use it. Ok? In your file you Inputs taking data input, output All you have to do is store all that data So that you can use it later. Ok? I hope that will be clear to you what is file. Ok? if simple words I said file is a container. a file box It is like. It's like a container. Ware Python Programs and data are stuck permanently On your computer. so don't disappear when The Program Ends. Ok? What is a file? It is like a box. It is like a container In which our Python program, What is that Python code? permanently It is stored. Ok? Our computers so that even if our program ends Even if it happens and disappears, what is ours What is data? Don't move away. Ok? I hope it is clear to you. Ok? and and We can also say that file is a named Location. Any one name has a location In secondary storage where we store the data What do you do? Store it permanently FOR LATER ACCESS. But later, if we If ever there is a need for it in the future we will I was able to access it on time. Ok? I Hope This will be clear to you. now the next is Types of files. Now how many types of files are there What happens? Computer Stores Every Files as a collection of zeros and ones that Is in a binary form. Look now at our There are different files in the computer It is of type. for example we have There is an Excel file, there is a CSB file, DOC is a doc file of Microsoft Word. Ok? It is a notepad file. Ok? But But the computer that stores our Everyone needs binary format that is zero and one It stores in my own. there for everybody A file is basically just a series of bytes Sturdy one after the other. Ok? so we say Can you guess what each file is? Just a series of bytes. Ok? Bytes is a series of what happens after each byte Used to be? The store is being stored. Ok? there are Mainly two types of data files. Text File and Binary File. Ok? Our There are mostly two types of computers in a computer system There is a file. text file and binary File. So what will we read first? Text File. Ok? a text file is a sequence of characters consisting of Alphabets, Numbers and Other Special Symbols. Ok? Now the text file would be is that what is it? it is a sequence of Characters. what is in the text file Is? There are only characters. Now Our characters can be anything. yes It could be our alphabets, our numbers yes, it is our special symbol, anything, all this Alphabets. Ok? then that text file what happens in? sequence of Characters. Ok? files with a Extensions like xt, py, dotcsv, etcetera R some examples of text files. Correct Is? What are examples of text files? are our?xt,py, dotcsv like this. What is this? Our This is an example of a text file. Ok? Bo But the computer does not store it like this We love it. Ok? Now our text If there is a file, what does it contain? There are characters. but as we We are looking at that computer system Doesn't store that way. now the thing Iz how does he store it now? so internally The Computer Store Everything as a Zero End One Called Binary. Now we are looking at Text form contains characters. But In whose form is this thing being stored? does it store? Computer stores In binary form. that is zero and one In the form of. Ok? Now see how he does it Is. each character like A, B, one, person, Whatever our character is, a numeric code using asky and Unicode. Ok? like ascii and unicode Which are our encoding schemes. Right? So What do these encoding schemes do? whatever Our characters have a special Gives a numeric code. Ok? Ok? They give the code. Now, like for example The letter A is what is its SI value? 65. Now convert the numeric code In which will it happen? in binary. like 65 convert Made it 100 0 into 1. Ok? In this Converted it. and in this binary format What does he do in it? Stores it. Ok? so computer stores a as 1 0 1 In the file. Ok? What will the computer do? This is represented as zero one in binary format. will store the form in the format. Now Now the thing is that it's okay. This has created a binary I stored it in. but when we click again If we do then we don't see binary. The text form we had left is the same It is visible. Is it or not? so when we open the File The Text Editor Converts This Binary Back into a readable character. Ok? We What does it do when you open it? Who Our computer is a system that uses text What is a text editor with the help of editor does it? which we converted to binary he was put back into human readable form I convert what we read into Gives. Ok? I hope this is clear to you What is a text file? So A text file is simply a collection of Sequence of characters. In this only Our data should be in the form of characters. Ok? Now the next is EOL. This is very This is an important thing. This is something to understand. What is EOL? eol each line of a text The file is terminated by a special character Called the EOL, end of the line. Ok? Now Whatever number of lines we have, whatever text file key for example this is our text The file is there. there's a line in it right here It is terminated. It is over. Ok? so here But what comes? There is a slash. What does it mean? our end of the line this character here It comes. There is a special character which This tells you that this line is over now. Is. Now whatever you want to write you can write it on another You will write it from the second line of the statement. Correct Is? So, what do you call this? end of the Line. In Python the default EOL character is slash n. What is there in Python? end of the line slash end that means here It's over. Now whatever comes from the next line Have to do it. Ok? this means start a new Line. What does it mean? start a new Line. that start a new line. it is all It is called a new line character. And What do we call EOL? New Line The characters also speak. Next Is Binary Files. What are binary files? text file in character form yes. Binary file in character form No. Ok? this binary file is a file that stores data in the form of zeros and ones once. Not as plain text. Plain Text The text file is ready. Now the files that Our data is in binary format that is zeros and ones What do you store in one form? Same what happens? Our binary files. Correct Is? Unlike text files which contain Readable characters. in binary files contain images, audio, video, software, Data Etcetera. what is in binary files Is? What does a text file contain? There is text, there are characters. But What do binary files contain? Like for Example our images, JPEG, JPG Images. Ok? What is this? Our file is All of them are there. Ok? There is audio, mp4, mp3 files what is this? These are our files. so this What is? Our binary file. there are videos Gone are the software data, et cetera. So What has happened to us? Binary files. Ok? Ok? Its examples are like Image file Audio file jpg, mp3 The executable file of the software extxc is the dot data of the raw binary data of PDF. Ok? in which all these our images audio all this comes should not be in the form of character Bus. Ok? these files are not human Readable. Now the characters are We will read it. butt which is binary form zero And what is it that is in the form of one It is not in human readable form. So Binary in our human readable form It does not happen. 10 Tips to Open a Binary file using a text editor will show some Garbage value. But still if we have binary Opened the file with a text editor. Suppose you have a text file that If you open it with notepad then it will be done and You can understand the text that is written in it I must be coming. But I have not placed any image file Is. Ok? jpg file is an image file I opened it with a text editor. So it will open even then but there will be nothing in it Such garbage AB anything symbols anything If it is written like this then what will it give us will give garbage value. Ok? we can read And write both text and binary files Through Python programs. Now we will learn Python Even normal text files can be opened through the program And even binary file which we will study next What can we do to them? Read on that and Wright can do both. now operation of Files. Now what do you see in the files? can you do the operation? like suppose i have this This is the real file. Ok? You will have it. By the way, I also have a real file. Now What can I do with this file? Am? I can open this file, I can close it. Now, whatever is in this file The data is written and I can read it Am. I can also write in this and I can also append to it. of append What does it mean? Like suppose there is something in it The lines are written. Now I after this I If I want to write something then I can write that too. Ok? And then close it at last I can, I can stop it. so similar It is there in this too. we are anyone who is our file In computer system we call it open, close, Can read, write and append. Ok? Let's move on. opening and closing a Text file. what shall we do now? One Will also create a text file. i you I will teach you, what else will we do? open this And I'll try both closes. Ok? Now in real world application computer Programs Deal With Data Coming From Different source like database, CSB files, html file, XML, JSN, et cetera. But first we have to open a file. Ok? what do we do real world computer In the program, what we get from the file The data you get is different sources such as databases, CSV file, HTML, XML, JSN file Available from different sources. But whatever files we deal with, What do we have to do first? That The file has to be opened. Opening is Mostant. Ok? Opening a file. to open A file in Python we use the open function. If we want to open a file in Python if so, what shall we do? open function in it Let's use the . The Open Function Takes Two Parameters that is file name and mode of access Mode. Now look at our open function What happens is he takes two parts. Which ones Parameters? One is the file name which we need to open Have to do it. Comma mode. Mode means the in the mode you want to open. Now more I will explain it well. The syntax is open of the function. Here the file object is a I will create it. Meaning where is the store after opening You are doing that. this file object is created Did. Open here. Ok? Now in the open Firstly, the file name which we want to open Is. comma access mode in which mode we are Have to access it. Ok? Now you will understand What is access mode? The access mode is n The argument that represents the mode in which the files has to be accessed by the Program. Ok? Meaning of Access Mode happens to be an argument that Represents the mode. What mode is it now? means it represents a mode that If you want to file then you have to open it. In which mode to do it. Ok? like real I will immediately save the file which I have Like for example this one which I have right now I have the file, can I even open it? I am immediately reading and writing as well I am doing it, I need to append something I can do it too, I have even closed it but This doesn't happen in Python if you did if you have opened it in read mode then you you can only read it okay now you put w write mode in it then you can just You can write but you can't read it again yes it is ok i hope it is clear to you has it is also referred to a processing mode ok access mode we call processing Also called mode. here mode means the operation for which the file has to be opened like for for reading, for writing, for both reading and Writing, for appearing at the end of a Texting file. The default is the read mode. Ok? What does mode mean here? Mode means that operation that such an operation In which you have to open the file. Ok? Like for read there will be only read. Right There will be only right for this. read and write It also happens for . For that we use mode Let's do R plus. There is also W+ which is right And it happens to Reed. Right? so this There are different modes. but now One thing to understand is that default mode is Read mode. that if you used the open function I did it, opened the file but in which mode did you choose it? If it is not written then what will the default mode understand? What is read mode for you? I have to read it. Ok? we can perform only that operation Which is in the mode. we do just that operation Can do which is in our mode. For Example here I have an f=open. One The file has been opened. So which one here have you opened the file? In which mode is the file dotxt? In read mode. in R mode. Ok? I Hope This will be clear to you. Ok? These are some more different types There are modes of. e.g. R B for R Reed to read but b is binary file for r plus and plus r r plus of It means that the read is there but a plus is added Then it will be right also. W this is only for right for WB plus WB plus means right and for binary. If it is plus then you can buy binary You can do both read and write. Ok? A A plus for append you can also append Yes, you can read it also. Ok? this some There are different types of modes. These What do you have to do? Have to study. Ok? So Let's go quickly to the computer screen Let's see Pay and Opening a File. ok dia students so firstly I put this file here I have created a file by doing h in python File. Now I will create a text file I will do it. Right? So look here we have Clicked. now look here where This file of yours has already been created. Ok? You have to create your text file right there. Have to do it. So click on create file here. do. Now I can do any kind of text file I will give you the name. Like I have here Gave me the computerxt. Ok? What did I do on my computer file Have done? I have opened it. So you see can you what is here? Open here The text file has opened. Right? Now We also add some data to it. For For example, I wrote here WE R Learning Python. Ok? I have added this. now what do I want to do it? This file has to be opened. Ok? So what did I do here? one here A file object has to be created with the name F. You, any You can take more names. There is no issue. Ok? now to open it we We will use the open function. open it here Gave. Now we will write the name of the file here. So What is the name of the file? Computerxt So let us write computer dotxt. Ok? Comma. Now I'm writing this In read mode. In read mode we just Can you do it? You can reed. Ok? When We have to write the file further and add anything to it. When you learn to do it, I will give you all that too I will teach you. Ok? So now see this I Opened the file. Ok? Now the file After opening, what do we have to do? This has to be run. Ok? so as soon as I ran so now look at me here No error has occurred. Why did the error not occur Is? Because my file has been opened. Right? If my file was not like this then my what comes here? An error would occur. Ok? Now Like suppose I have it here on the computer from computer name so here is the text I have created this file. Now what am I I will write computer science here Am. Ok? Now I know my computer Any computer science name in system The file is not like that. does not exist I would do it. So as soon as I see you now I run If I do this I will get an error. that File not found error. that there is no such file Not there. You want to open a file like this Yes, it does not even exist. Ok? I hope this is clear to you. I this again I will remove it from. one more thing guys that if Suppose you don't have the option to open this file That suppose your Python file is So this one is here. Like I've written in Python I have created a folder, it is there. butt but Your text file is being created somewhere else Is. So you don't have to worry about it. Like this is computerxt. go to this file You have to click. And here's an option It is coming by copy path. you have to copy the path And then you have to enter the full path here. Have to copy and paste it. Ok? So What will you have when you run it? will it be? Your file will open. Correct Is? Now you can see there is an error here It has been happening. Syntax error and Unicode error. Right? So why is this error coming here? Because this is what we have read like we I have also read in previous classes that slash It is n. Ok? back slash n. Ok? What does this happen for? This happens to our Any for a newline. Ok? whatever We write back slash n. like for For example, if I wrote this then why? It happens? For my next line. Sorry What does this back slash n stand for? for the next line. So here what this slashes are coming this is also ours this is this I am considering it. So what should we do? simple slash here this back slash n After removing it, we will put a normal slash. Correct Is? This is the complete file that users can use in C going to magnet brains then I did a I have made Python. Folder named Python And then I typed a computer name in it The file has been created by computer.xt. Ok? now this is what I doubled the back slash n Did it with a slash. so now i open it I will do it. So look, my file is now open. Is. I am not getting any error. Ok? I hope this is clear to you. I Hope it is clear to you. Ok? now the Next is closing a file. Now the file I have learnt how to open it. How to close? Right? So, once we are done with the read right Operations on a file, it is a good practice To close the file. Python provides a Close method to do so. while closing a the file, the system freezes the memory allocated To it. Ok? Like what did we do? Any read/write operation can be performed on your file I did it. You can also read your text file I took it and even made it right. So this is a good There is practice on what we do to the file Do it? Close it. Ok? If we close If you don't do it then there is no problem It may happen but it is possible that our file remains open It is gone, someone else should come and make changes in it. yes whatever data we want to store in it Those things are not getting saved. Ok? or else it be whatever data we have stored in it He may get corrupted. Right? so good for this The practice is that we close the file Always do it. Ok? Ok? Now Python Like in there is open method for open. In the same way for close we have There is a close method. Ok? Whenever we When we close it, the memory that was allocated what is that file? becomes free Is. Ok? Let us now look at the syntax of Cloze Let's see. File object dot close. Who We also opened the file in which the object What do we make him do? Close Let us get it done. Simple. here file object is an object that was returned while Opening a file. Or what is a file object? in which you opened the file Object. Ok? as an example of this f = open as in abcxt opened the file with a in plus mode give here f this is what I did I created the file object and gave it to you f dot close okay here I have placed the file Closed it okay so let's go Quickly show it to him on the computer screen Let us apply this file now. I had opened it, will just close it because it is a good practice this is very there is good practice so that our No one can make changes to the file. Ok? Like if this file is opened then the file which We have opened the object and in that we I will close it. Ok? like like this We did the app dot close here. For this We use the close function. Ok? I closed it using app.close And then we will run it. Ok? See We ran so we had no errors I. Why did the error not occur? because the file just Ours became open and then ours became close Went. Ok? I hope this is clear to you. I hope this is clear to you. Let's move forward Let's increase. now the next is opening a file Using a with clause. Now till now you have opened I tried using it with a function. open the file I did it. Now comes opening a file with Clause. that now we can open the file with a name Of is our keyword or clause. We You can do it through that also. now now The question comes that maam we can do it through open function I was doing it. but now what is this with clause Did you need it? So why was there a need? see Are. Ok? in python we can also open a File using a with clause. The Syntax of the With clause. Syntax of First With Clause Let's see. With first With keyword then Also using the open open function. Vid then what are you doing in open then open? file mode file name sorry what we have to do Access mode in which to open Edge A File object. Ok? what do you do in it Were? Earlier we used to take file object but in this There is a with clause. I will write it this way. Ok? Let us go. The Advantage of Using With Clause Is that any file that is open using this clause is closed automatically. what does it mean Its? that if we open any file with clause so what do we need to do with it There is no need to close. this automatically It gets closed. The file which we open Using function we can do it without with clause What if we open a file It happens? We had to close it manually Is. Ok? once the control comes outside the With clause. As soon as the control is ours comes outside the clause, in case what is it It happens? My file gets closed automatically She goes. Ok? In Case the User Forgets to close the file explicitly and if an exception akar the file is closed Automatically. If you are using with and If the user forgets to close the file If I forgot to close it manually then or Then what came to us? any exception come What will we have if it goes away? File Because we opened with a with clause It gets closed automatically. Ok? I Hope it is clear to you. Ok? this one Simp and it provides a simpler syntax. What is the simple syntax of With With It happens to us too. Ok? like this with open this my file.xt read in plus mode as f name We opened it here with the file object Is. Ok? here we don't have to close the Close the File Explicitly Using Statement. Python will automatically close the File. Python will close it automatically. Ok? So let's go quickly on the computer screen. this too practically Let's see. Ok? So now we have opened the file I learned how to do it, I learned how to close it. Now We will learn how to open a file using the with clause How to open it? Ok? So let's go, this We took this file from the computer, same file will take it. We have written something like this in it Is. Ok? So what do we do with it Are? first with first with our keyword will come. Ok? Then with us open open. Then In this we will write in open the file name which we Want to open it. So what do we open Do you want to do it? computer.xt so here I did taken computer.xt Ok? this file is opened in comma mode If you want to do it then I have read it here I have switched it to read mode ok sss File Object You can use any file object If you want to give a name, that is fine, then I have included it in I gave it to you and told you I want to do it in F that's ok then after this we will put a colon otherwise We will get an error, okay now watch the video By opening it in the internet we get to know something or the other Some more operation will be required here Like now it is not the case that we can save the file as normal Should I open it or close it now? You will have to do something here, read or write If you need it, I will open it in read mode So what should I do? Whatever the data is I will read it. Ok? So here we have did that content Equ f dot read. Ok? that in the file Read whatever data we have. Simple. Ok? Our data can be read from here Will go. Now as soon as we run. so here But our run was done. we have no error She has not come. Ok? Now here we There is no need to use the close method. because we are opening with with so this It also closes automatically. Ok? The content has been read from here. If I do this If I get the content printed then whatever I have data that is written in we are learning Python hello so this is also mine and it will be printed Will go. Ok? But right now we're not doing it Are. Because when we are on the topic of read We will then go after doing all these operations We will see. Ok? I hope this is clear to you will be. I hope you have understood this. Ok? Let's move on. Now Writing To a text file. What can we do now? We can also write in a text file. So far we have only learned to open the file. Learned how to close. Ok? Now to the text For writing text to a file in python We first need to write to the file To open it in a write mode and append mode. now is it ok? What do we have to do now? For example, if we have read mode then in that only We will read. But we need to learn to do it right Is. So for right we always say R means sorry WW means right mode and A What do you mean? Append mode. Always file in the same What does one have to do? It has to be opened. Ok? If we open existing file in right mode, the previous data will be erased and the The file object and the pointer will be positioned at The Beginning of the File. What does this mean? that look here you have this much in your mind I have to make you understand that you don't have the right here. Here Whenever you hear the meaning of right, it What does it mean? Overite. Now what is overite? Right and overright. so this Hai right means that if I If you open the file in write mode then whatever All my data that is here will be deleted. Everything will be gone and I will start over Now whatever I write here will be like this. Ok? Like for example here I have I have opened some file by using abtxt. Correct Is? So what was in it first? hi by one There was a message. Ok? There was some data after saying hi. Now as soon as I put it in the right mode What happened if I opened it in W mode? All my data is gone. then after that which I also wrote the data, it starts from here Our writing will go away. Ok? Now one of these There was a discussion about that pointer, what is a file pointer Is? Position beginning of the file. that which This is our pointer, it goes to the beginning Is. In the starting. so what does it mean Its? This means that as I have seen this What will happen if opened in write mode? All data is gone. It's erased, it's gone. So, we have a pointer. Correct Is? Like for example what is the pointer now Is? If it is in the last then what is this? At once You will reach here at the start Pointer. So my writing starts from here. Will go. Ok? now look ahead and see You will understand it well. Ok? Now If we open it in append mode. These It is the same file. By saying hi. Now this I What? Opened in append mode in A mode. So what does append mean? Adding that Any file that contains text is data. Keep adding it at the end. Ok? So here as soon as I put it in A mode I opened it so what is my pointer now? This went to the end, where should I go now? Do you want to write? I have to write from the last. Ok? Now If the writing mode was from the beginning Went to the starting. not like that Start writing from here, in the middle Will go. No. So what's in it now? In this Where is our pointer for writing 'a' gone? after hi just say hello everyone like this From here. Ok? I hope this is clear to you will be. Ok? so if we open existing file in append mode new data will be added at the end of the previous data as the The file object is at the end of the file. Ok? file object or pointer to it What is? If we save the data in append mode Let's open a file in append mode in C So our existing data is It remains as it is. The data is as it is will remain. Like hi it was, look as it is. Now from now onwards whenever we add some data Once you do it, it will start getting added. Ok? And where did our file pointer go? goes? At the end of the file. end of file In. or the file which is at the end of the last In. Ok? I hope it is clear to you. Ok? Now we will also see how to write it. How to write in a file. so this is We learned in mode what type of mode We have to keep it open. mean mode should be when We want to do it right. want to write in your file. Now to write it down One we have is the right method. now right What is the method? The Right Method Takes An string as an argument and writes it To a text file. now what is the right method does it? Takes just a string. One Takes only string. means it takes one line and adds it to our file. As For example, let's take a data file and see opened ib.txt in w mode right in mode. we did data dot right what we did Did? did you write it in the file? Hi Everyone. So just what does it do? One takes a string and adds lines to it one by one Does. Ok? so to add string in The Next Line We have to add a new line Character at the end of every line. Now What do we have to do? If there is any data then Next If we want to add it in the line then what should we do? Will it need? You will have to put slash n here. Like hello everybody? then I said how are you When I wrote it I put a slash n here. If I I will not put slash n so here hello Everyone. just next to that here's how you are You? All this will start getting written. Correct Is? Now we will see it practically also. I Hope this will be clear to you. so when you Use the write function in Python to write data into the file the data is not immediately Saved to your hard disk. Now this too in What happens is that if I write any data I have done this in my file so now it is not like this That my data will be saved immediately. So Where will my data go? instead of it first written to a temporary area in a Memory memory called buffer. What is this? Who We have written the data. Whatever the data It is written earlier that it is directly permanent Will not go into storage. first one of our Is there a temporary area nearby? Buffer. will go into the buffer. where do we go after this Will you pay? After this ours will go In permanent storage. Ok? Look now Like it is also written in it when you close the Function. As soon as you close the function yes. Ok? Python when you use the close Function means the one which closes the file You use the function. Python Text The Data from the buffer. will take data from the buffer and We store it permanently in our secondary storage I will store it in. Ok? Example understand. I opened this file. Ok? Then what did I do here? I did it right. So when I write, what is this? going into the buffer. and when I closed it If it is there then what is ours? permanently Going into storage. Ok? i hope it It is clear to you. Ok? so this is right now saw. Now let us look at the right lines further. Then we both should do it together practically We will see. Ok? So look at the right side What used to happen? a string a string We put it. So he used to do it right. now right What happens in the lines? in the right lines What is? we put a lot of lines together We can mix it and put it in and that is our right It will happen. Ok? See how? this method Used to write multiple strings to a File. If you want to write a line then right but Multiple lines or multiple strings If you want to write then write lines. Ok? we need to Pass an Iterable object. what about us? An iterable object such as a list It's gone, it's become a tuple, what's in it? Have strings. He tells us the right lines method Have to pass from. What does this mean? Look, I opened this file. Now Look, I have taken this list called lines Took. Ok? Now in this list this slash n What is slash n lug? I have lots of The lines have been taken. I have taken the first one Hello Everyone. Next, I took up writing Multi-line strings. then take the second third I took it. This is a third line. three line I have to take it. Ok? someone in a list You can also take it in a tuple. and then I have mentioned it here in right lines. The object is passed. so now like As soon as I close the file, my There will be a file, what is in it? Very Multiple What are lines? She will come in writing. Ok? Let's go quickly to the computer screen Pay. Let's apply this as well. ok dia Students. So what do we do first? We will create the file. a text file We will create the . and then what did we do do? Then we use our right method do. What does the right method come for? Anything To write, whatever we want to write For him. Ok? So first of all we Let's create the file. Ok? so like That for example I have created a text file I have created it. here I gave it Student. Create a file named student.txt Took. The text file has been created. So now What will I do next? Some of it I Let me add the lines. Like I added I did it. Hi How are you? Ok? we are learning Python This way. Ok? I wrote this text file I did it. Now we know, first of all, if we If we want to use the right method then we should How to open the file? to the file or so in the right method sorry in the right mode or Then you will have to open it in append mode. Correct Is? so here I did f = open that Open the file. first of all here what comes? Name of the file. Ok? I have copied the file name here Students. so we'll write that student.xt comma now in which mode i want to open then Right is to open in W mode. Correct Is? So right then what are we going to do? This f dot close close will do it. Ok? Now! I am not writing right. Ok? Bus You just have to see what changes come From write mode and in append mode. Ok? So I had explained the write mode to you guys. Right vote means to overwrite that whoever is in my student and text Whatever is written here, remove it and then our Whatever we write will be written. Ok? So now you look carefully. here i am right I will open it in mode now. not running yet Did. Then I will write it here. that f Dot right. Ok? Make it right. what right do it? Here I wrote a supposition I Magnet brakes were added here. Only Right now we are taking only one string. Ok? I did this. So look now as I I will run it, my program has been run. And I went here to my text file. Ok? On the students. so you can see whatever was written whatever three three four What are those lines that I added? Overite are done. It has been removed. And I here But it was written magnet brains, that is right Already happened. Ok? I hope this is clear to you will be. Ok? Now you look carefully. What I want to add one more line so I have to type here But the same will have to be added here again. Now Look I have again posted the Magnet Brains ad Did. Now I ran. now i went to the file Magnet brains and magnets again Brens has arrived. Right? Now if I get the next If I want to write this in line then here I have What do I have to apply? slash en next line Character for. Ok? See now I I ran. Now if you go then see the next My magnet brakes are back in line. Correct Is? I hope this is clear to you. Ok? So what happened to us? Right method. Now What do you do? Let us comment on this. Now let us open the same file. Ok? Beans Let's open the file. but now we are open Will do A means in append mode. then append What happens in the mode? whatever you wrote It happens after that I start writing Is. Ok? like you did in append mode And here I did f dot right k right Do it. What should I write? Here I have Wrote a that having fun. you can write anything yes. I wrote about Having Fun and Then here What did I do to the file here? App Dot close closed the file. Ok? Now you look carefully. As soon as I run I will do it, I scored this run. this is mine There is a program. I ran this and I went here now to the students file. so now look here after the brains It is written having fun. Ok? so i Hope you have understood that the pointer How do we know what right means Overite. remove whatever is written there then write. Append means whatever is written Write it down since it happened. Ok? Now you look carefully. now its already here So much having fun is written in append mode. Ok? Now I put a slash forward here I put 'N' to go to the next line. Correct Is? So now you see as soon as I ran So look, having fun has already been written once Is. Right? So it does not remove it. whether a Once the bar is run then it is written. Ok? After that it is again in the next line Having fun has arrived. Ok? I Hope You must have understood that we call the right method How to use it? Ok? If I have any If you want to add more lines then we can do that too Can be done. Like suppose I have put three here Added more lines. Here I wrote Khurre I wrote here We are in India. Ok? Here I wrote I love Cold coffee. Wrote anything in this manner. End Then I ran. So now as you look at I am a student, all this is in the text Things have been added to us again. Ok? It has come appended. Ok? I Hope this is clear to you. Having a Funny Third Why did the time come? because I already gave it The bar was added. then i ran it again It is like this. Ok? Now look as I what should I do? If I removed the append mode Gave. Turned to right mode. so whatever All this must have been written, this is from above right? It will be removed. Just having one who last four Lines we have just added 1 2 3 4 Bus That will remain the same. Look, we scored a run. now gone Look at the file, everything on top has been removed. just who These are the last four lines, they have come to us Are. Ok? I hope this is clear to you. One more thing if it ever happens to you that If you are opening this file but it is opening It does not happen. So sometimes this may be the reason The python file may be in the folder you are in there is your text file.txt file has not been created. so what are you to do Is? Nothing simple. go to file here Is. Right click and select the option here Copy path will appear. Ok? you are here now The full path instead of the full file name I have to copy it to my C folder I have faith in users in C drive Python in desktop in folder in Bachori I have to go then. Right? I hope you like this It will be clear. one more thing sometimes like this What happens is that this slash here is your There might be some error. So if you have If you get some error of this type then you can do some simple You should not do it. Don't panic. give here but remove this slash and remove the back slash Just write a normal slash. Ok? because we black slash generaly like slash N is for next line or escape Use it for the character. that's why just What do you have to do simply? normal here A slash has to be put. Ok? So now I've I have also told you all the conditions that your Under what conditions will the file open? error in what This can come so that you can solve it easily. Ok? I hope this is clear to you. So Let us now look at the right line method. As It was right. In the right method, we only do one You can add a string. Right Line Methods are used. In this we have multiple You can add strings for that. Correct Is? Let's get started. First of all Let's open the file. f = open. Ok? I am this student and the txt file which is available now I had made it, I would have opened it only Am. Ok? I opened it. Ok? And now I'm opening it up In append mode. In A mode. Ok? so which Look, it is written here, after this we Multiple lines will be added to us. Correct Is? so now here I did a I have made a list by lines In which we have to add whatever we have. Ok? So I want the next line If it is in, then let's write slash n. Here But I wrote we are Learning Python For the then next line I wrote Computer Science. Computer Science then write for the next line I love Maggi then next i wrote slush n WhatsApp ok what i did is simple one I have taken the list, ok I have taken this list now I want to add this to this so we'll do f dot look at the right lines its already coming here Write lines. And in this, we have these lines There are multiple lines from the name, we have I added it. and then at last what do we do do? Not cluttering your files I will forget it. Because it is a good practice. Ok? Look, as soon as I ran This is in my student file Everything came added. we are learning Python, Computer Science. I love Maggie. And This space came in front because I typed it While doing this, I have left a space here. That's why I left it after the slash end That's why. Ok? here comes the. Now see append in mode. Now let's do it in write mode. Now As soon as we do it in the right mode, whatever Whatever we had written, everything worked Will go. Watch Now Bear Learning Python computer science i love maggie whatsapp all this It has arrived. Ok? So I hope you are clear where would you use the right method and Where are right line methods used? I Hope this is clear to you. Let's move on Are. Now the next is reading from a Text file. What have you done so far? to the file I opened it and closed it. Right in that too I did it. Right? What do we do now? Readings From a file. If the file needs to be read. Ok? so to read a file, we must make Sure that the file is open in. Ok? If If we want to read the file then In which should it be open? R mode, read mode, R Plus Mode. R Plus. The R means Reed. What does plus mean? Right Also. Read and write mode. W Plus. W means right. If there is a plus Wright as well as Reed. Ok? and a Plus mode. What is A plus? A meant is append mode. If there is plus then add read also in it It happens. Ok? now there are three ways to Read the contents of a file. What are we now? The contents of the file can be read in three ways Are. Which ones? First is read method. now what is this read method does it? The Read Method Returns the Specified number of bytes from the file The default is -1 which means the whole file. Like now what about the read method? for example I put 10 in it. Ok? Arguments Passed 10. So what will he do? 10 He will read the characters and give them to me. Correct Is? So what is this? As many as specified number of bytes we need, read that many and give it to us Gives. and its by default This value is -1. Now what does this mean? This means that if we have any Value not passed through this. Right? so this what will he do? this is in the whole file Read all the characters and give them will give. Ok? As an example this is one What have we done? my object is a file has been opened. abtxt in read mode. Correct Is? then i put my object here dot read 10 10 read it. 10 characters. Ok? So look, this is the file. In this This was a text and I read it 10 times. I have been. 10 characters. So make it count. 1 2 3 4 5 6 7 8 9 10 Hello ever. this is so much What will the text do to us? read it and give it will give. So if we print the output then How much reading will you give us? Hi Ever. Ok? And right now if I say something here I should not pass it either. any argument here If I don't give it to him, what will he do? As much as it is these are the characters, read them all and give them to me will give. Ok? I hope this is clear to you will be. Let's go quickly to the computer Let's apply this on the screen. Ok So students, let's see what we read first Method. Ok? So for the read method, the best First we have to open the file which In? In read mode. Ok? So this which student and there is a txt file with hello Everyone? How are you? We are learning Python, Computer Science, I Love Maggi, WhatsApp. These I have already added some text. Ok? So let's open the file. So I have selected open in file object named f Did. You can do it in any other way too. Such There is no problem. Ok? Ok? then go to student.txt here Opened this file. In which? Read Mode In. in R mode. In addition, if you Open in W mode or any other mode If you do this then it will not read it. Ok? donation What do we do? f dot read from file. We Whatever it is, let's get it read. Correct Is? Now after getting it read we will store it somewhere do. so here I stored it From a variable data name. Ok? and then At last what will we do? close the file I will get it done. Ok? Ok? Now look like As soon as I run it, this data has been read. Correct Is? No I don't have any error. Data Read Done. But you will say ma'am this is visible I am no more there. So how will this look? because when We will not print our data until read We won't see it until it happens. so like The data is read from here, we will do it here Print data. that whatever data is read What should you do to him? You print it. Correct Is? So look, like I told you, data.fab.read what is it used for happens to have a by default of -1 That reads the entire data. so like If I run it, you will see that it reads the entire data It's done and it's here. whatever data is printed He was there. Ok? now what did I do here Did? Here in read I did it 10. So whatever these starting 10 characters are yes, he will read it. Look hello ever. Correct Is? The 10 characters in Hello Everyone Hello ever comes in that. So this one read it I gave it. Ok? I hope this is clear to you will be. I hope it is clear to you. Let go It's read. Next is read line method. Now You must have understood from the read line. Reed Reads the character and reads it completely Is. read line so what does this mean? These Reads just one line. Let's see Are. The Read Line Method Reads One Line From The File. This will delete just one line from the file Reed does. you can also specify how many bites from the lines to return by Using the size parameter. now you can see whether it contains read line or size parameter is n right so if we use this So you can see this by default Example is by default so what will one do He will read the same line but if I have done this if I put size four then what will it do if There are four lines in my file so all of them It will read it and give it to you, okay its syntax is file dot read line and whatever size it is If you want to enter it, it is fine, like now I have put the data opened the file and read the txt and then I did f data dot read line only If I pass any arguments to a read line Just one line will be read by me. Ok? Now I put three here. So what will happen? The three starting lines If yes, she will read it and come to me. Correct Is? I hope it is clear to you. by which we What do you do? Let's read the line. Let's go quickly to the computer screen Pay. Let us apply it. ok dia Students? So now let's look at the read line method. Are. What does read line method do? Only Reads a line. Ok? Let's go to this Let's try it. First of all what do we do? We will open the file. This is the student file which we have just Students have created this file, we will open it do. Ok? Now we will open it In the read method. Ok? because we need to read Have to do it. And then we will do data = f Dot read line. Ok? what does read line do Is? Reads only one line. Now I As soon as I print the data here Then we must not forget to close the file. Because it is a very good practice. Ok? So look, in this read line we are writing anything It is written, let's add something to it first hello How are you We are learning CS like this ok these two I added the line and then I said hello give it this way hi okay we did it diya ok now what do we do read line I did it so as soon as I ran look you can see hello how are you joe The first line is the only one I have read It has come yes. Ok? Now here I am I added three. So now it's not like I have to Three lines will be read. starting of I have read those three characters It will be done and come back. Ok? I hope you like this It will be clear. Ok? Look, this is what we did I have done the read line method. now we read line We have come to know so much from the method that what we Can you do it? can read line by line Are. Ok? Now we will complete each line With the help of the loop while reading, we can complete the You can also read the file. how to read Can you? Look, here we have seen this file I opened it. Ok? Now we can make our loop I will apply vile tru. Meaning of True True What is? until I reach at last This loop has to be run again and again until it reaches the end. So then what did we do? Now we read Let's get it done. I read one line Got it done. Earlier data was taken from name. Now I took the line in it and made the app here Dot read line. Ok? So what happened? My line got read. Ok? Now After this there is a line break somewhere So, it means the line will also end. So Here we will do the if not line. If Not Line what does it mean? if the line is not found It is that no line is found to be read. So what should I do? break the loop then do Two breaks. what should I do otherwise? which is our There is a line, what should I do with it? Print it. So print two lines here. Ok? But Now whatever line we get here, Ok? we want them to be different in that There should be separate breaks for proper lines Available in format. that is why here we are String has a method called strip that I will use it. Ok? Now look, as soon as I I will run here as much as I have lines are one two three four read line method Every line has been read out. Correct Is? I hope this is clear to you. watch now What do I do from here? removes the strip yes once. So look it is read but this This extra gap of each line has come. Right? So, to remove this, we what have you done? We have put read line dot here I have stripped. Ok? I hope you like this It will be clear. I hope this is clear to you. now the next is Read lines method. Now see the meaning of read line What is? One line. Read lines. Meaning Multiple lines. When all the lines are at once If I want to read, then we use read Lines. the method reads all the lines and Returns Along the Lines with the New Line As a list of strings. what does it do Is? Read all these lines does it and gives it to everybody in the form of Is? List of strings. of string List. Ok? sorry yes string key Gives it to us in list form. As For example, we opened this file. I read the lines. Got it printed. So The output will come. You can see whatever output comes You can. Hello everyone one line. How are you? Second line. Ok? we live in india Sorry. Hello everyone? How are you? One line. we live in india second line ye third line in a fourth line so can you see this ho is returning in the form of a list Right then he reads the lines but But all the lines are in list form okay i hope this is clear to you If yes then let's go quickly to the computer Let's apply it on the screen. Ok. So Next we have read lines now. Now What does Read Lines do? any number of lines he reads them and makes a list Returns to us in form. Ok? Let's see. We opened the file. We have opened this student.txt file. have been. Ok? and do it in read mode Hence the R mode. Ok? and here it is now I've done data = f dot read lines. That Read all the lines. And then what did I do? Get a print out Gave. Whatever lines we have read are at the end Then lastly we have to close the file I must not forget this. Ok? so you can see It has 1 2 3 4 lines in which this is something It is written. Now as soon as I run All the lines will be read and come to me And it came to me printed. and you look at You may find that this has come in list form. Ok? Because why? because we read Lines method has been used here. Ok? I hope this is clear to you. I hope it is Clear to you. Let's move on. Next Is setting offsets in a file. Now before Will you understand what offsets are? This is the offset of The discussion is going on. so look like for Example one: this is our text file and There is some text written in it. Ok? So now if I write this file Opened in W mode. so right We know the meaning of mode, the meaning of right What is? overwrite whatever is written I have to remove this and then write it here again. so one we have an offset or we can say Yes, we have a pointer. like this We have put all the text in this entire file Opened it in write mode. Sub text What is? It will be removed. So now it's not the case that My text will start from here. no where is my text here? STARTING Writing will begin from. From here Ok? So start writing from the beginning here. will be. so then what will happen is that The pointer, the offset, can reach here. will go here. Ok? what have I done now? Now I have Any one else can send this file to me There was a file. Now in which file did I open it Did? I opened the file in append mode In. Ok? Which one did I do it in? Append in mode. So what will happen with append mode? In append mode we know the data is anything It does not get deleted. It is added at the end. So now what if my pointer comes here? will it happen? So this will happen from the beginning. Then it will be wrong. Will not be able to append. append means at last so here is my is ending. So after this, from here on my The pointer will be added to the start last. So This is called a pointer or you can say The offset where we need to place our pointer I have to go. Right? So for this we need the data To access. When we have random data It is in fashion. Right now we are starting from I was taking something like this till the end. Butt if I need the data from the middle of the file From. Ok? Need it from the second line. Only I need a second line. Right? need to read Second line. So for this I have two methods There are. A seek method and a tell function. Ok? The seek method and the tell method. Both Let's see. Tale. What does the tail do? The Tale Method. What does tail mean? Narrate. So this also tells this. The Tell Method Returns the current file position in a file Stream. that our offset in the file is or we can say which is our pointer What is his position? It is in the starting, Is it in the middle, is it in the last, where is it? So What does this tell us? Tell method. As Opened the file. Then we filed I read these lines, even if one line is read. After reading one line, look at it by reading the line Like I read this one line in the file Took. So when one line is read then my pointer I went here. Ok? so something here There will be characters. if you walk this close here Went. Ok? so now i'm done here print f dot tail tell me where the pointer is Is? So whatever its position will be from here He will return it. Ok? So let's go Let's quickly go to this computer screen. Let's apply this as well. So let's go to the tale Let's look at the method. First of all this is Student and txt file. Some of it in Our lines are already added here. You See Can we live in India? we are learning CS. It's a good time for learning. Happy Birthday to you this way. Ok? So What do we do with the file now? Firstly open We do. Ok? like I did here diya student dotxt Ok. Now here I am going to put it in read mode I am opening it. Ok? be careful Please see. Now I said here that print do it what? f dot tail. Ok? now f dot Tale. What does the tale tell? And to the last Don’t forget to close the file. so this f dot tail tells us where our pointer is Is it on? So what happens in read mode? It happens in the beginning. because this It reads everything right from the beginning. Right? So look here, as soon as I hit run If you do, it will come to me in print Zero because it is at zero position. Ok? Now what did I do here? Now I am a Let me read the line. Ok? Such as I'm done here that F dot read Line. Ok? What will happen by reading the line? Here is my pointer from here He read the first line till here. Now It will come here. after that the next line will go here. Ok? read a line Took. So my pointer is already after A. Already happened. V1 India A finishes one line behind It was done after that. Ok? So now I've If you get it printed then you can see that it is 18 bat it is happening that wherever these characters are Wherever there are any number of finishes There it is coming on 18 characters. Ok? I hope this is clear to you. Correct Is? Now what do I do with this app dot read line do I do it? I will comment on this. Now I put your file in A mode i.e. append I will open it. in append you know The cursor here is our pointer It already happens that it goes in the last. Whatever you want to add in the end He does it. Ok? I did the run. So now Look, here the value of our pointer is this Sari is coming. All the characters in 87 he is the most here because this He has gone in the last. Ok? Now this we You can also open W mode in write mode Are. Ok? So here I have w mode then we know that W It is not written but overwritten. everything would have been it goes away and the pointer is our cursor It goes into starting. Look at whatever There was something and it went away in our file From. Now zero has come here. why come zero Went? Because here in the beginning our It comes. Ok? I hope this is clear to you will be. I hope tell method will be clear to you. Now the next is seek method. Now Seek Method What is? The seek method sets the current file Position in a file stream. tell me the tale where is our pointer? And if I set the pointer to a particular location Have to do it. So what do we use here for this? We do? Seek method. what is its syntax Is? File dot seek offset. Now what is the offset Is? Offside is that number. He is number one Where you have to place your pointer at that position Have to set it. Ok? so a number Representing the position to the set the current Stream position. the position at which the pointer is The number we need to set is our offset. Correct Is? Like opened the example file. Here Pe what f dot c fourth. So now what's mine The pointer is on the fourth character Will go. 1 2 3 wherever there is fourth character I will go there. Ok? let's go It is quickly available on the computer screen. This also apply. Ok? So let's see This is our seek method. What we do in seek method We do? Wherever the pointer takes us It happens, we take it there. Look now We already know this student girl There is a file in it, something is already written here It has happened. I have added some sentences. Hello everyone hi hello hey. Ok? So now What did I do here? read the file Did. So as soon as we are in read mode we know which is our pointer in the beginning It happens. because in the beginning Reads everything from the beginning. Now What did I do here? I did Given f.c C5 Or let's make it four. four after four Go. Ok? So now my pointer was It will go on four here. 1 2 3 4 after the starting four characters will go here from O. what do i do now Am? Anything now because it is open in read mode If yes then I will read the data and see it. Whatever there is. so here I did diya f dot read whatever there was read Took it. And then I printed it here given data. Ok? Is? and then at last We must not forget to close the file. Because it is a good practice. Ok? So Now look carefully. As soon as I run, Now from here it is read O everyone. Hi It has not happened since. O it has been read by everyone. Why has it been read? Because that's my point. I went from seek to already four pay Was. That's because mine arrived after four Is. That's why. Ok? I hope you understand He might have come. just now we read earlier that the tale What happens in a method? pointer in which position it is on it tells us and by the seek method we It shows where we need to move the cursor Have to get it done. in whichever position we want We can take it there. I hope you This will be clear. Let's move on. Now Next is creating a file and writing data. what shall we do now? create the file and write data into it. Let's see. so to create a text file we use the open method and provide the The file name and the mode x. Whenever if we A text file has to be created. Ok? So what do we have to do? Us You have to use the open method and the file has to be copied in it Name file name means the name you want to give yes. Ok? Like for example you can use the file Whatever name you want to give to the file, that should be it You will keep it. Name Doge And Mode X And Mode What will always be there? will remain X. such as for example your file name you gave the name Done name file and gave x mode here. Correct Is? now if the file already exists with the Same name the open function will behave differently That is gives error. Ok? what does it mean Its? that suppose you can open the file here You are getting the demo file. Ok? What are you doing? yes? You are keeping the demo file name. but already You may also want to create a file for this file. had been. and the name of this file is demo File. another file with the same name as the demo file It already exists. Ok? so here what is on? Do not keep the same name for two files Can be done. so what is here Will she go? We will get an error. Ok? So We have to keep this in mind. Right now this We will see it practically also. next if we create new file in mode write and append then open a function behaves differently Depends on the mode. Ok? Look now What's in it? Look, we are going in X mode You can create it. Ok? Now his Along with this we can also create with w or a mode Can do it. How can it be done? What does it mean This is what if we did that to the file? Opened in W mode. Ok? Right Opened in mode. So what does right mode do? gives? For example, understand it like this I've got f=open. Ok? Open the file by doing this ab dot text Did it and here it is W mode. Ok? So What now? Now it can be done by doing ab.txt The file already exists. Ok? Or She won't do it again. both are these options Can be done. Now we will understand the first option that could be ab.txt file It exists in our system. If If she does this in the system then what will she do? Just like there is a right mode in this There is no need to create any new file. just who We already have this file, right? Make changes to it It is just that. Ok? like the file that is in it whatever text is there because it is in write mode then the whole text will be gone and now what we You will add data, add it here from the beginning Will be done. Ok? Now if we append this done in mode. Ok? append a here What will happen in the mode? Our data as it is Whatever is there will remain there later on from now on It will start. Ok? This is then Condition when our file already exists She does. But now suppose I have AB.txt name None of these files exist. So now If I put this in W or A mode then I will open it. Ok? file exists only No, I will not do it but I am opening it. So my What will pass? A new empty file It will be created and come. Ok? So we this method also with an x mode or else this What can we even do with the mode? Understanding Can be done. Ok? Let's go quickly on the computer screen. do it practically Let's apply. ok diy students file Let's learn to create it. See the most What will we do first? We will open the file. So I took the variable from a file name itself Took it. the data object that we speak of and We will open the file. Ok? now here But I need to give you a file name that It does not exist. Ok? so here But I suppose I named the file Magnet Brance dotXT. Ok? is there any such file It does not exist in my system. And now I want to create this, so I will create this In which should I open it? In X mode. Ok? And then lastly I will put the file in file dot Let me close this as well. Ok? So You look carefully, here I have put a magnet brensxt if I show you the file here I have someone here by the name of MM also you can see a magnet by the name brens None of the files exist. Correct Is? what have I done now? I ran this. Ok? I ran. So as soon as I ran if you do then you can see the magnet brensxt By doing this the file has been created for me. So Whenever we need to create a file We always open it in X mode. Ok? I hope this is clear to you. Now What? Now we can do this only through X mode No it opens in W mode or A mode also Can do it. How? like for example I took this. Now any other file Let's take the name of F here. Ok? Now Magnet brains already exist here She does. So now I will get an error. Right? So now I took some more files Name of. I took cs dotxt. Ok? Now let's go here to C. So you see You can do it here with C name or CS name I don't have anyone. Just Clean and Color There are two files with the name. Ok? So now as soon as I will open it in W mode. Correct Is? Even if the file is in w mode it is in write mode I am opening the door but still right Even if we are opening it in mode Even if we do not have the file Even if I run, I will have the file It will be created and come. Ok? See like When I ran it, I copied the csrtt file to my It has come closer. Ok? I hope you like this It will be clear. Similar to append mode also Even if we do it, the same thing will happen. Now I am here I am doing it with the name cs1. cs is cs1 It is not by name. Ok? now run kya apeng mode so again I have a file It has been created and has arrived. Ok? I Hope It will be clear to you. but but we want this That it should be done in read mode, will not happen. Correct Is? Like I did here in R mode I am here to put CS2 in read mode I am doing it. Ok? So I ran. So Now look, there is an error here. Did an error occur? That file was not found. what type of file It is not there and does not exist. You Reed You are trying to do it. Right? So always You saw that right now we are going to open any file in X mode. Create a file or write it and From append mode. Ok? i hope it is Clear to you. I hope this is clear to you also will be. Ok? And if you like di students If you have any kind of doubt then please comment so that I can resolve your query I can do it and explain it to you very well I can Ok? Let's see the next one. Traversing a file and displaying data. We have a file and we need to traverse it and displaying the data. what is this? Two Traverse a file, the file will be opened in Read mode and reading will be begin from the Beginning of the file. If we need to modify a file I have to traverse every single character If we want to go to the beginning we always have to go to the beginning The files must be opened in read mode and Always start from the beginning Will have to read it. we can also use loop For traversing at each character of file. And what can we do? also use loop Can be done. If we have to pay attention to each and every character If you want to go then For example, understand the file like this Object. We open a file in read mode In. Ok? from a string here we have what did you do? I read it. just read one line till. Just read one line. Ok? These There is a file and read only one line from here Did. Now we run the loop that while STR That is as long as STR is there. Ok? As long as it means What is? This is the string at the end There is a slash. did not run to last character Till then I go. what do you do till then? This Print it. and then again new line Read it. then one line got printed, The second line will be read. print a line gone, the second line will be read. And so on. This until our slash ran to n By the time I leave, this will be done. Ok? Ok? And then at last we should have this close Will go. Ok? I will close the file. Ok? Let's go to big computers On the screen. Let's apply that as well. Let us now learn traversing a file to do. Traversing means one by one Read the characters' lines one by one. Ok? which we read through the loop do. Look at this, this is a CS dots file now I had created it. I have some of it in Added the lines. Hello everybody, happy Birthday. I am healthy and wealthy. God loves me. Ok? This is how I here Some lines have been added. Ok? You First of all, we will open the file. So we opened the file cs dotxt and because we have to read so read mode. Ok? Now you carefully See. If I do it here like this, that line equal f dot read line that read a line do it. And then what should I get done here? Let me print the line. Ok? and file We must not forget to close it. Ok? So you can see I only have what will happen one line will be read starting key hello everyone everything is ok I am not able to understand but now I have to read all the lines If we want to do this then what will we do for this loop okay so I put a loop that while True that as long as it is true it means it is true okay It is true as long as it is true meaning again and again To keep our loop running repeatedly First of all what we will do is write a line Let's read it first, I have written here what did you do, read that line = f dot read line Ok? Now as soon as we read the file So now this is the line that is being read here Now it will end somewhere or the other. read line somewhere Reed will end where we end. So what do we do for this? Condition We will put the if not line. that what is this? If As soon as our whole last is gone Till there. if you don't get it then here what should I do? Break. Ok? But if you get it If it goes then what should we do? means where It has to be printed till there. So What can we get done here? Get a print out Will you give it? Line. Ok? what do we do with this line here Will you give it? I will get it printed. close the file You must not forget to do it. Ok? So Look, I have all the lines I have come back after reading it. Hello everyone. Happy Birthday. I am healthy and wealthy. cold Loves me all this. but you can see this There is also space coming underneath. Right? So What can we do for this so that this We have extra space, it should be removed. Its So here we will use strip. What does Stripe do? further down the line The extra space behind the line Leave it and remove it. so look here now Hello everyone happy birthday. I am healthy And wealthy. We have read all this I have come. Ok? So I hope you enjoy traversing You must have understood how to do it in the file. I Hope you understand traversing. Ok? Now we have the next one We traversed it and also displayed it I did it. Now the next is pickle module. Now what is the pickle module? So in Python V some need to save the objects on the Disc for later use. In Python we sometimes What do we do, what do we have to do? We need to save our object to disk. It happens. has to be saved permanently so that we can never do anything about it later Do it? You can use it. this can be done using Python pickle. We call it a python pickle, a pickle. There is a module, we can use it through it. Ok? Now Python pickle module is used for serializing and deserializing a Python Object Structure. Ok? If If we have a Python object, it Serializing Python objects and What does deserializing do to us? Requires the pickle module. Now this what is serialize deserialize I will tell you everything. one more thing ye joe pickle Modules are used for binary files It is done. Ok? any object in Python can be pickled so that it can be Saved on disk. Ok? Any object which is pickled in Python, that's because This is done so that it remains permanently embedded in the disc It can be saved. what pickle does is it Serialize the object first before Writing to a file. What does he do? Pickle what happens from this? our first object It is serialized. Ok? then some of our It is right. What happens now is Serialize He looks at it. Ok? So serialize is a Process serialization is a process of Transforming the Data and an Object into the memory ram to stream of bytes called Byte Streams. What does this mean? Serialization is a process that It transforms. What Transform does it? which is our object or our is there any data in stream bytes It converts the . Ok? Stream Bytes or we can say we can say that we can call this binary what does it do in the format? converts Is. Ok? the data, the objects, our It is stored in the RAM memory What does it transform into? Binary In format. or we can say byte In streams. these byte streams in Binary files can then be stared at in a Disc. what happens now? this byte There are streams, then disks are stored in a database at any in a database and or in a network Through. Ok? End Serialization Process is also called pickling. and Serialization We also call this process pickling. Are. Ok? I hope this is clear to you. Ok? Now let's see the next one and we And you will understand it better. Look now Deserialization and understanding. What does the serialization symbol mean? Who Any Python file in our memory Transform the object or data What to give in? in binary format or In byte streams. Now Its Just The Same The reverse deserialization occurs. donation Pickling Deserialization Serialization So unpickling deserialization is the Inverse of pickling process. Ok? These What is? This is just the opposite of pickling Of the process. Where a Byte Stream Is Converted back to a Python object. in which What happens? it is a byte stream convert it back to a python object Is given. Ok? The Pickle Module Deals With binary files. Pickle module is used in Does she deal through? Who does she deal with? Deals with binary files. here data r not retained but dumped and similar data is not read but loaded. Now look at our binary Cannot write data into the file. Right? So, you can't do it right. Therefore we We tell him that we are dumping the data Are. Now let us open the binary file. So Now if we open any image then we can see it It has been like that only. I can't read it. So What do we say here instead of Reed? Let's say load. Ok? so give it here Methods are used. That is dump and load. Ok? Let us now take this image as an example Let us understand from this. See What is Serialization? Any object can be converted into a byte stream Converted it. This is serialization. Now what did we do? The byte to this Converting streams to binary format Understands it well. then put it in the file or Stored in a database or in memory. Ok? Now from here again from the database Only byte streams will be output. But we understand does it come in? Normal object. Normal convert the data then back to byte stream did it in the object. So we'll tell him What? Deserialization. Ok? I hope this is clear to you. Let go Now the next is dump method. Hum Dump Method We will also see the and then load method. Correct Is? Let go. This method is used to convert Pickling Python Objects for Writing Data in a binary file. the file in which Data are to be dumped needs to be open in a What does binary write mode WB mean This means that whoever uses this method what is it used for converting to convert that to our python object is it in binary file or Let us say in simple words what our Python object to write anything in it okay for that which is in our binary file If we have to write anything then we will do it for that What do you use here? Is the dump method okay? it is there but we will not write it right, we will say it here Dump its syntax is Dump Data Object Data objects are objects that give us the data we need All you have to do is dump the data and do it here what is a file object what is a file object that the file in which the dump is to be made is correct so this is what I told you where is data object what is the object that has to be dumped that We need to dump the data and the file is here what is that file object here that we have to handle the file okay Let us understand this with an example, look at the import pickle was imported earlier now If you want to dump data then list is given here I took some value in one geetika f 26 This data has some value to me now what to do, write, dump I have to do it, okay, so I took this data ok now what i did is open the file I have opened the file by my binary dot data binary file open key wb mode yes We must take care to keep the right binary correct w means right because we're writing dumping and b means binary file on binary file If you are using it then open it in WB mode it's very important okay so here we have But you have taken this data which needs to be dumped Take the file you want to dump here Took it. Then we put pickle dot dump. The data that needs to be dumped here is Have to do it. And then we closed the file here. I did it. It's done, simple. Ok? So let's go Let's quickly go to the computer screen. Let us apply it. Let's look at the dump Method. So what do we do now? Pickle Module Through which what happens to us? Binary There are files, what do we store data in them do? Will dump it. Ok? So first of all Because we are using the pickle module So we will get it imported. Import pickle. We will import the pickle module. Correct Is? Then after that whatever we want to dump They take the data. So for example I Here an LI can get the list by name Am. in which I take any value I will take it. I took the forest. Then I I took any one name here. I took the Took devotion. Ok? then after that i took it here Lia Female F Ok? and then I took it Took 26 here. Ok? take any number Took it. You can have something else if you want. I have also written here that Hello, you can write anything with this type. Ok? So what are we supposed to do? These We need to dump the list which is our binary file We will open it. Ok? now what now do? We will open our file. So Here I have done File = Open. Correct Is? Now look at our binary file. Ok? If we call it binary here Gave any name to the file. like for Example A, I have given the name here Demo Dot D AT what happened to my binary file Demo.it does not exist anymore which is my binary file demo binary file but because I want to create this Even if it happens automatically then what should I do about it I will open it in WB mode We know that if the write mode is set to write In binary file mode if we open the file if we do then what happens even if we it does not exist but the file is still there It will be created automatically. Ok? I Hope this is clear to you. what to do now Will you give it? I will dump it. that pickle dot dump Dump it. Ok? Dump what? so which We have to dump it. So why dump me Is? List by LI name. so I put that Gave. Ok? where is the comma? Through whom? I mean dump it in what? want to do it? This is the file which I opened I have done it in it, right? so we will do it here File. Ok? Done. And then at last we Don’t forget to close the file. So we I will do file dot close. Ok? file dot close. Ok? i you I will show you. Like Demo Dot Demo binary file here a b c d. Here you can see it with D name Whatever the file, this is a Python file Is. But our binary file is not here Has been made. Ok? Look now I like As soon as I ran it, the program started running. So see my demo file here automatically It has been created. Ok? now here it is The file has been created. what to do now Are? Let's open this. look at it like that what happens to it? we need an editor Is. Ok? So that's why when we open it Do it like this demo. Ok? sorry this The demo text file has been opened. Now we Here is the demo which is our file containing the data. Here it is. So we opened it. Look now You are coming here open anyway because We'll need an editor. But still I took it here which is built in Editor is a text editor, open it through that Let's do it. So you can see I Shraddha F Hello all this is what I did here I have arrived. Ok? Now you will say that maam here But why did this 1 26 not come? it's because I did not come because what is this? Our Integer Is. so because we have a particular one There is no editor so this is all here But could show the binary file. Ok? Because what does it mostly do? Supports text files only. But From here you might have got the idea that we What have you done? all these things here in our file because this is a binary file Is. It is getting opened. I mean, this list What are the items we have put in the Have you done it? have been dumped. Meaning The binary file has been written. Correct Is? You also have to practice this. Correct Is? I hope this is clear to you. I Hope This will also be clear to you. now the next is Load Method. What is the mean of load? this method Is used to load and unpickling data from A binary file. When we retrieve the data Have to do it. From the binary file again we get What is? I need my object. Ok? Unpickling has to be done. Ok? us here Have to display. so what are we here do? We will use the load method. the file is to be loaded is open in binary read mode Only. Now we are reading the file. Right? If you are displaying then read only Have to do it. So Reed is doing butt binary The file is there. so what should be our mode Needed? RB Mode. Its syntax is store object = LoadFileObject. Ok? What is this here? are loading. What Are you loading? File object. the file which The file from which we need to extract the data And even after loading this the data is nowhere There must be a store somewhere. So this is one of our The new one will be a store object. Ok? Its Let us see examples. then practically I will apply. Ok? first pickle I imported it. then here's a print Got it done. The data that we are storing in files R. Then we open this file object Open the binary file in RB mode. End Then we loaded it using the pickle. If you load it and store it somewhere then It is stored here. but right now just the store It has happened. It was not printed. to display What do we have to do for this? Print have to do it. so what do we do here Will you give it? I will print it. Ok? I Hope This will be clear to you. So let's go quickly on the computer screen and to him too Let's apply. Ok? So let's see Now the load method. The load method of the pickle module. So we know that our binary file You have to read it. what data is in it is it ours? Read it. we read it I do not say anything. What do we call that? To load. Ok? So that load method we have here But there are learners. Ok? See now We just saw the dump method so from there we do does did? We dumped this list Was. That means we added our this in it In the binary file that we created. Ok? what shall we do now? I will comment on this I will give it to you. Now I am going to tell you this same binary I downloaded the file from here demo.a data.dat whatever data i have dumped in it There is already a store. What will we do with him? will load it. Ok? So firstly import Our pickle will definitely come. Ok? This I have commented. so here it is again We did it. what shall we do now? First I will get it printed. Let me print a message. here before Pay that's the data data Starved in Binary The file is. Ok? Here we print a normal message Got it done. What do we do now? the file which We will have to open it. Ok? so what are we do? Re-open demo dot data do. Butt will no longer do this in WV mode. in read mode because we need to read write sorry read binary rb means read What will we do with this in the binary file? open I will do it. Now after opening, what do we do? We do? We need to load. So here it is What will we do? Pickle dot load. Ok? What will we do now? I will load it. What Which one to load? whatever is in this file That is it. Ok? Now we want that this The data which gets loaded should be printed let's go. So what do we do for this? This Once I did R.E.S. here and got the result And stored it in a variable. Now What do we have to do? will print it out So that we can understand it better. Ok? here so that it will print So that whatever data we load gets printed Will go. And then at last we have to file Don't forget to close it. So here it is file dot close. Ok? So this is it. Now let's run. So look Here comes the data student binary file Is. We had dumped this same list here. Shraddha F26 Hello To One Shraddha F26 Hello So this file has reached us. Ok? I Hope it is clear to you that we can use load method How to use using pickle module. I hope it is clear to you. Ok? So Last Our topic is file handling using pickle Mode. Now we will understand this practically. What will Simple do? which is our pickle module with that we can do basic operation of any binary what will you do on the file? I will apply. Correct Is? So let's do this, what should we do? First Let's take a look at this. As We Read and Write Data in a text file. similarly we will Adding and displaying data for a Binary file. like we read and write Are. Any file is above the text file Performs read and write operations. in the same way We can also do this on binary files. Correct Is? Let us understand it practically. let's go Quickly on the computer screen. For him Let's apply. ok students? So let's go We will perform our basic operations on binary files We do. Ok? so first of all here But we need to import our pickle module Will have to get it done. so import pickle pickle Let's import the module. Then its What will we do after that? We will open the file. The file should open. then open the file Let's take it. that file is equal to open and open which We also have to give a name. Ok? such as for I have given the example cs dot data. Correct Is? So, I have made this binary file. Now because this type of binary file in my It does not even exist. So for this we What to do? we will do this wb in mode means in write binary mode. Right what happens in binary mode is that which is already a even if the file does not exist That too would have already been created in the right mode Is. Now this file has opened. now to open What will we do after that? add some data to it do. Right? Will add the data. So its So we took a list and in it we Put something in. Hi Guys. Ok? then some more in the list wrote How are you? Ok? So one thing is open Mode. What will you do now? anything we keep in our file I am writing. which is a binary file I am writing in that. Ok? so right We know what we use to do this Are? We use our dump method. Correct Is? So what do we do now? through pickle Will dump it into a binary file. Pickle Dot Dump. Ok? Now what do we have to dump? Meaning, what do I have to write? So this is the L.I. There is a list of names, dump this data Have to do it. Ok? comma in which mode? File From the mode. Ok? This is in the file. Simple. Ok? What will happen now? Our The data will be dumped. Ok? what to do now Are? Let's score the runs. If you did a run then look now This was run successfully. so cs dot this By doing CS binary the file will be created with me. Look, it is ready. Ok? Now I opened it If I did it, it came open anyway. So open anyway. donation Here we will use the same text editor. so you can see here hi guys how Are you? What has happened? We have an add on to it Has gone. Ok? I hope this is clear to you will be. This has been added. No problem. come now Let's go again. Now it has been added here. Now we want to see. Come on this is ours It is in the binary file. But now we have our Python We will look at what is here in the file I want that whatever we have put here in the file Has he dumped me exactly or not? It has happened. So what will Simple do? First of all Don't forget to close the file. whether it is a binary to close, whether our Be it a text file, it is the same. so here what happened to it? We became close. Ok? Now what should we do next? Here from After closing After closing Now we will open the file. butt but then It will open in but read mode. because now Till then it was in write mode. So RB here. Correct Is? When a binary file needs to be read So just what happens to us? RB Mode. Meaning read binary mode. Ok? Now Binary What do we have to do in the mode? have to read So reading means getting the data from there So we call that load method. Ok? That is called loading the data. So we What to do? We will load the data into it. That pickle dot load. Ok? Load. Now which In? This is the file we have opened Loads from that. From here. Ok? so here it is What will happen? Our data will be loaded. Ok? Now after the data is loaded we want This data has been loaded. butt print Is. That's why here on our Python file It will show on the screen. So what do we do about this do? Show a result by name here I will take it. We will store it here. and then We will print the result. Ok? End At last again when the file is opened again If you have done it then close it again I must not forget this. Ok? Now let's run. I ran. So look here hi guys? how Are you? This came to me with the data loaded Is. Ok? I hope this is clear to you. So This is the basic operation to open the data. to the file. dumping the data to the Loading data end at last file To close. These are our basic operations have been done. Ok? I hope you like pickle module And all the operations related to that we have done Do it, they will be clear to you. now what do you want to do it? You need to practice a lot so that you get it very clear Let the concept be done. Ok? And if you If you have any kind of doubt then please comment so that I can resolve your query I can do it. So now we will meet in our next session And this is where our session ends And as you know magnet brains Provides you high quality Education absolutely free of cost from Class KG to Class 12 You and many more Magnet brains for watching videos You can subscribe to the channel Magnet You can download brens app or You can also visit the official website. Magnets provides you with top Courses as well as English and Hindi Medium Classes for the Various Board Likes CBSE Board, Delhi Board, Bihar Board, UP Board, MP Board etc. and if You need notes and e-books and the link is Given in the description box. then see you We are in our next session. till then Keep laughing, keep smiling.