In this tutorial, we shall be looking at file handling in Python. What is file handling, how can we do it and what all operations we can perform are some pf the few questions to be answered.
Let us start by understanding about a file. We all have witnessed files in some or the other way talking about software files like word file, excel file or a hard file like your project file. File is a collection of miscellaneous data. Same is the concept with the files in Python, you can write in your file, create a file, read from your file, make changes in your file, etc.
There are basically two kinds of files that are widely used in programming namely, “Binary” and “text” files. The binary file as its name suggests consists of binary bits that is, zeros and ones and the text file consist of text written inside it.
Let us now look at how we can open and close a file.
Here in Python we are provided by the open() function which opens the file for us in whichever mode we want (we will talk about it in this module).
Let us look at the syntax of the open function.
Variable = open(file_name, mode_of_file, buffering)
file_name: this could be any string value.
mode_of_file: it specifies that in which mode you want your interpreter to open the file. Let us look at some modes that are provided by Python.
r mode -> the file is opened in the read mode only (this means that you cannot write inside the file but can read its contents). If you forget to mention any mode, then the file will be taking the read mode as default. By using this mode, you can read the contents of the file from the beginning.
rb mode -> in this mode the file is opened in a binary format for only reading. You can read the content of the file from the beginning.
r+ mode -> in this mode the file is opened in both reading and writing mode that is, you can read the file and write in it simultaneously, from the beginning of the file.
rb+ mode -> in this mode the binary file is opened in both read and write mode. The pointer of the file is placed at its beginning.
w mode -> this mode opens the file in writing mode only that is, you can only write in the file and cannot read its contents. It will overwrite if the file exists. One of its advantage is that that it creates a new file in which you can write if the file does not exist.
wb mode-> in this mode the file in opened in a binary format in the writing mode only. It will overwrite in the file if it exists and if not, then it will create a new file.
w+ -> in this mode the file is opened in both write and read mode that is, you can write inside the file and read it simultaneously. It also overwrites existing file and create a new file if it is not present in it.
wb+ mode -> in this mode the file is opened in both read and write mode, in the binary format. It also overwrites existing file and create a new file if it is not present in it.
a mode -> in this mode the file is opened in the append mode that is, the pointer of the file moves to the end of the file if the file exists and if it does not exist, then it creates a new file.
a+ mode -> in this mode the file is opened in append mode and reading mode. The pointer of the file moves to the end of the file if the file exists and if it does not exist, then it creates a new file for both reading and writing.
ab+ -> in this mode the file is opened in binary format for appending and reading. The pointer of the file moves to the end of the file if the file exists and if it does not exist, then it creates a new file for both reading and writing.
Now let us look at how we can close a file.
It is very simple; here we have a close() function that we use for closing the file.
There are some of the functions which we can perform from the object of the file.
Let us take a look at them.
To get the name of the file-> f_object.name
To check weather the file is closed or not -> f_object.closed
To know the opening mode of the file -> f_object.mode
Let us now look at a code to gat a better understanding of the code
r = open("sample_file","w+") print('Name of the file is:',r.name) print('Is the file closed?',r.closed) print('Opening mode of the file is:',r.mode)
Name of the file is: sample_file
Is the file closed? False
Opening mode of the file is: w+
Let us now look at how can we read and write in a file.
For reading from a file we have the read() function which reads from an open file. The function reads both string as well as text data.
For writing in the file we have the write() function which writes in the file and it can write both binary and text data. It does not put a new line at the end of the string.
Let us look at an example to get an better understanding.
def one() : print('-----------------WELCOME------------------') a = input(print('Enter your Name:')) b = input(print('Enter Your roll number')) c = input(print('Enter Examination name')) d = input(print('Enter Date of Examination')) f1 = open('efile','w+') f1.write(a+'\n') f1.write(b+'\n') f1.write(c+'\n') f1.write(d+'\n') f1.close() f1 = open('efile','r') print('Name Is :') print(f1.readline()) print('Roll No Is:') print(f1.readline()) print('Name Of The Examination Is:') print(f1.readline()) print('Date Of Examination Is:') print(f1.read()) f1.close()
Enter your Name:
Enter Your roll number
Enter Examination name
Enter Date of Examination
Name Is :
Roll No Is:
Name Of The Examination Is:
Date Of Examination Is:
In the above code, we are taking input from the user and writing them in the file named efile, and then we close the file. Then we open the same file in read mode and print the data of the file on the screen.
Here, we are using readline() function which enables us to read data line by line.
Let us look at how can we rename a file.
We have a function rename() which enables us to change the name of a file.
We need to import the os module first.
If you want to delete a file then we have a function named remove() which is used to delete a file.
That is it for this tutorial, I hope that you are cleared with the concepts of file handling and now you can try it out by yourself and explore.
See you in the next one!