File Handling in Python |Episode #12|
Hello and Welcome to Python Programming for beginners, where we teach you Python from Scratch. In the last python class, we learn about one of the most important topics in python programming, Functions, and Recursions. If you haven’t checked it out, CLICK HERE.
Starting with today’s class, our today’s topic in the Python Programming series is File I/O or File Handling.
File Handling in Python
The Random Access Memory is volatile in nature and all its contents are lost once a program terminates or the power goes down.
In order to persist the data forever, we use files.
A file is data stored in a storage device. A python program can talk to the file by reading content from it.
Types of Files
- Text Files – Files whose data is readable to us. Ex – .txt, .c etc
- Binary Files – Files whose data is unreadable to us. Ex – .jpg, .dat, etc
Python has a lot of functions for reading, writing, updating, and deleting the files.
Opening a File in Python
Python has an open() function for opening files. It takes 2 parameters: filename and mode.
open (“this.txt”, “r”)
here “this.txt” is our file name and “r” is our mode, which is reading.
Reading a File in Python
Here we use the open function to open a file named “this.txt” with read mode, then we read the content of the file using, f.read() function and then we print the text written in the file and then we close the file using f.close() function. Remember, to close the file with the f.close() function as the file should be closed after it has been used.
We can also specify the no. of characters in read() function like f.read(2). It will print two characters from the file.
Readline() – Readline() function is used to read a full line at a time. Ex:
Modes of Opening a File
- “r” – Read mode.
- ”w” – Write mode.
- “a” – Append mode.
- “+” – for updating
- “x” – for creating the file it isn’t there
- “rb” – It will open the file in binary mode
- “rt” – It will open a file in text mode. The default is “rt” mode only.
Writing a File in Python
In order to write in a file first, we will open a file in write or append mode, depending on what you want to do. Then write in the content of the file using, f.write() function. Let me show you how.
Here we first open a file in write mode then we write in the content using the f.write function and then close the file. One thing noted while using write mode is that it overwrites the file as in this case, in the previous example, we do have some text written in the file. But opening it in write mode, it overwrites and deletes the content of the file. It also will create a file if that doesn’t exist.
Appending a File in Python
If you don’t want to overwrite a file every time you open it, then use append mode to add the contents to the file. Let me show you how
Using With blocks for File Handling
The best way to open and close the file is by using with statement. Ex:
Here we use with statement to open a file as f and then read the file using f.read() function. The biggest plus point of using with statement is that we don’t have to use the f.close() function anymore to close the file. With handles it automatically for us.
So, that’s it for Today’s class. If you want to know more about modes and File handling, CLICK HERE. For a revision, we learned about File Handling in Python today, we saw how to open a file and what are different modes of opening a file. Plus, we saw how to read, write, and append to a file. And in the end, we end with the “With Statement”. So, that’s all for today, This is Dolores Haze, signing off, I will meet you in the next one, till then Bye and “Happy Coding”.