1.18 File handling in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav covers Python file handling with the open() function and context managers. Learn three reading methods (read, readline, readlines), how to write files, and how to work with CSV and JSON formats using their respective modules.
Chapters
Ladies and gentlemen, so in this video we are going to see what file handling is. File handling in Python is very important skill, we can say, for interacting with external data. You're learning Python not just to program for the short things, like small things, right? You're going to use this Python maybe for website development, maybe for game development, or maybe for data science or machine learning models and all. So everywhere you're going to work with external data, maybe through website, maybe with different, uh, file extensions and all. So file handling in Python is very important for interacting with external data. This file handling consists of multiple things, like from reading to writing to the file, or working with the different structures, working with the different file formats. So one by one we are going to see each of the things. So the first thing that we have is the reading from the file. Suppose now what I am going to do is I'm going to create one file. So over here I'm going to create one file, any name whatever you wanted to. So let's work with the txt file. So I'm going to say exercise.txt. So now I have the normal file which is with the name exercise.txt. Now I'm going to store one something, just a random line we can store. Python is the programming language, programming language which we are learning right now. So now I have saved this file. Let us Then create a new file because now we are going to work with the file handling. filehandling.ipynb. Okay, so now I will tell you how to open any file. So the very important thing is to read any file. So in file handling, we do have open function to open any file. So whenever you are going to use that, you will say with with is— with, you will use with as a keyword, and then you will say open, and you will write down your file name. Here my file name is exercise.txt. Now I wanted to open this file to read it, so there are different modes to open it. So r is for read. Now I will say as files. So I've written with open as file. Now what I want to do, I want to read content. There are 3 ways to read it. One is using read, another one is using readline, another one is using readlines. So there are 3 different ways to use it. To read it. So I am going to say, let's suppose content is equal to— now I have the file in the files variable, so I will say files.read. I have written files.read. Read is also a function, so I have used the brackets. Through which we can read whatever we have written. Okay, so the extension was wrong, it was txt. Now if I say print content, it's gonna print the content for me so that I can read it. So I have written with— open is a function It's a variable which— whatever you wanted to write, you can. Content is the variable again in which I am going to read the file. Then I am printing whatever I have read it. So suppose I am writing— I am going to change, modify this file. Writing programming. Now I have 3 lines in my file. So let's see what I am going to get with the read. With read, I am getting entire content. Now let's try with the line. With the line, I'm getting only the first line. If I'm going to lines, it's gonna use all the lines which are available, but using the slash and keeping it all in the one line. And because it is creating a list of the content by separating them using the commas, so that is how you can read any file. Another task that you should learn is the writing files, writing on files. So writing is the task basically that we are going to C. So how to write is I will use the same command which I have used over here, but in this case writing, I'm not going to read it. So I will use W. This should be the code. So let's change it. Yeah, so with open write as a write file as this. Now whatever you wanted to write, you can write. Suppose I'm creating output.txt. It's an empty file, so I will use output over here. This is the different file that I'm using right now. Now I want to write files.write Hello learners. So let's go back and check. Yes, the content has been added because we opened the file using the write mode and we asked it to write this particular line for us. So that is how you can write on any file. So it's very simple to read and write. You just remember few functions that has to be used. There are different file formats that you can work with. One of the commonly used is the CSV file, which we also say comma-separated values file. So comma-separated value files also you can use, you can open using this. So whenever you're working with any different file structure, what you will say is you will say import CSV, import JSON. Like this you will import whatever file you are going to work for. Suppose now I'm going to create x.example.csv. Now I'm going to create another one, example.json. So now I have two files, one is with the CSV, one another one is with the JSON. So let us see, first I will run the command which is saying that import both the libraries. JSON is also a library and CSV is also a library. So in CSV you will mostly, when you are going to work with the data— like work as a data analyst, data scientist, or machine learning, you will work with the CSV a lot. So same way, what we will do is we will use the same command over here. Our file name is example.csv, and I'm going to open it in the read mode, saying that files.read I'm going to store it in C, and then I'm going to print. Now I'm going to quickly add some values over here: name, age, crop. So now I have stored quickly some data in it, and let's see. We are getting— we are able to read the CSV file. Now I'm going to use the same command. I'm going to open the JSON file. The JSON file name is also example. So let us see. Okay, we don't have anything in the JSON file. So if you are going to add something in the JSON file, you will get it as an output. I have not saved it, and this is not the correct structure of the JSON as well. So whenever you're going to work with the JSON file, if you will print it, you will get the outputs. So that was a part of the file handling in Python.










