Hello guys. Welcome to LearnVern. In the last topic we saw the Math module.
In today’s video we will work with File Handling.
What is File Handling?
File Handling is a very important part of Web Application in which we can store our data, we can read the data, we can update the data & we can delete it as well.
But there are certain permissions in the file.
As in what kind of work are you going to do with the file.
1st permission is R. R stands for Read a file content.
2nd is A. A stands for Append the value in file.
3rd is W, with which you can write in a file.
Now what can we do with the file?
We will learn how to create a file, read a file, append a file, store a list into file & read list from file.
We are going to see all of this practically.
Let’s move onto the practical part.
This is our Jupyter which you already are very well aware of now.
First of all, I’ll markdown.
First of all, we have to create a file.
Until you don’t create a file, you cannot start your work.
You can’t store nor read or update or delete it.
Let’s see how to create a file.
There is an open method to create a file which has 2 parameters, 1st is File name & 2nd is permission.
This is the syntax of the open method.
Now how to use this open method?
I have a string here. This is my file handling program.
I want to store this string in a file.
First of all, I’ll have to create a file to store it.
I took a variable named file. File handling has an open method which I’ll use here.
This is Python’s predefined method. 2 parameters can be passed here.
1st is the file name. I’ll enter a filename that is demo 1 dot txt.
Now what do I have to do with the file?
I have to store that is write this string in a file.
To write, we have this permission of W.
You just have to write W & it will automatically understand that you want to write in Python’s file.
There is a write method. What do you want to write? String.
So I’ll pass S here.
The Write method will store our string in file.
Now we will print a message that says “file created”.
Always remember to close your file otherwise your data won’t be written.
For that, you have to use the Close method.
What did we do? We took a string and we will store that string in a file.
Firstly, we opened the file that was created with the Write permission..
In which we used the write method to store the string. And then closed the file.
Now when we run it, we should get the message File Created here.
We got that message.
Now where would the file be stored?
You don’t have to worry. Just open Jupyter’s homepage & you’ll find the file there.
If you don’t want to open it from here then you can find it in the folder of Jupyter as well.
Where all your programs are being stored.
Now if you click on the file, the string has appeared in this file.
This is how you can create a file.
Moving ahead on how to read the file.
I’ll write here “File Read”.
We took Write permission with the Open method because we wanted to write in the file.
Now we will use the Open method once again & take the Read permission.
But remember one thing that is whenever you are going to read, you must have that file.
If you won’t have that file, if you won’t have that file, it will show an error saying File Not Found.
Always keep in mind that you should have the file on your PC.
Now moving onto how to read.
I took a variable named file again.
Used the open method once again & wrote the name of that file which we had created.
But the permission I’ll give is R. R stands for Read the file.
We have to read the data in the file & print it in the console here.
For that, I’ll store the data in a variable.
Filecontent = . There is a Read method used to read the file.
Write file . (dot) read. That’s it. That’s all you got to write.
And all the data in your file will be stored in Filecontent.
Now you just have to print filecontent, the variable you created..
Now I'll Run it and you can see that the data got printed from the console.
The one we had stored in the file.
So this is how you can read a file.
Next is how to store a list in the file.
I’ll write in Markdown. Write a list into file.
First of all I’ll make a list here.
L1 = 1, 2, 3, 4...rather I’ll take a string here.
Python, Java, Php and Angular. Our list is made of 4 strings.
But I’ll tell you one thing, when you are going to store a list in a file, you’ve written the list within the square brackets like Python, Java, Php & Angular, but when it will be stored in the file, it will be stored in a single string.
Why? Because when it goes in file in Python, it stores it under Single Block Memory.
Let’s check how to store the list in file.
I’ll take a file variable. Open. I’ll make a new file Demo2 . (dot) txt.
And open it under W mode. Why W?
Because we want to write this list in a file.
We have a method to store a list in the file…
It is the Writelines method. We have to store L1 in writelines.
And I’ll write a message here “File Created”.
And here, file . (dot) close.
We will run it & you can see that our file is created here.
Going back to Jupyter’s homepage & we can see demo2 dot txt file here.
We’ll click on it & you can see it got stored in a single string.
But even when you’ll Read it, it will be stored in a single string only
We shall see that as well.
I’ll markdown again. Read a List from File.
We will use the file = open method again.
Our file’s name is changed...why? Because we have stored our list into a new file.
Demo2 . (dot) txt and we will open it under R mode again.
I’ll take a file list named variable where the list we will read, will be stored.
File . (dot) readlines method. Readlines method.
Now we will print a filelist.
You can see that the string returned but in the form of Single string.
So this is how you can store & read in the file. Okay?
Next up we will see how Append in the file.
I’ll markdown once again, Appending the value into the file.
I’ll take a string here, Python file handling.
We have to store this string in our demo1.txt in which our data already exists.
It will store here at the end of the already existing data.
It will store this Python File handling here at the end.
Append’s concept in itself is to add the value at the end.
We will open the file once again. Which one?
The demo1.txt one in A mode.
A stands for Append. It is a permission through which we can append in our file.
We will use the file.write method because we want to write, only our permission is changed which is Append.
We will pass S. I’ll print here File Updated.
And then close the file.
Run it & the file is updated.
We will refresh it a bit & you can see that our data is appended.
So this is how our File Handling works.
We will see what we learned today.
We learned how to create a file, how to read a file, we stored a list in file as well as we read a list from file. We even Appended the value in a file.
This is how File handling works in Python.
We will see Real World Examples where we use File Handling.
Such as WhatsApp. In WhatsApp, when we attach documents...that is also one type of File Handling as we attach a file & then send it.
Same goes for Zoom. Zoom is used for meetings nowadays.
If you have to share a file in a meeting, there is an option to attach a file in Zoom as well.
Another one is Team. Microsoft Team.
Microsoft Team is in a huge demand now for meetings, school, colleges where lectures are taken & have to share the documents with students or candidates in meetings.
Here File Handling is used.
So these were some real world examples where File handling is used for General use.
In the next video, we see Exception handling.
Share a personalized message with your friends.