File Handling in Python
File Handling in Python
File handling in Python is a very critical and beneficial area for programmers coding as it is the cornerstone for performing writing and reading operations in a file. Performing ‘read’ and ‘write’ operations are the most extensively costly operations that one can perform and so attempting to optimize the operation of even a single program can positively affect the overall turnaround time of an entire application. Performance is known to increase drastically if one knows to efficiently handle the file handling options in Python while programming. So let us start with the basics to enhance further understanding.
Meaning of the term – ‘File’
A file is an entity that is given a unique name to store data records for the sole purpose of storage and retrieval as and when needed. They can be kept stored for indefinite amounts of time in devices like hard disc or they can also be placed in volatile memory like random access memory (RAM). In the case, if you need to view or perform read or write operations, you have to first open the file. And when all the necessary actions are finished performing, we have to close it to indicate the closure of actions.
Opening a file :
In order to perform read or write operations, we first use the open() function to open a file. As the end result, the open() function gives a handle or a file object. The following syntax can be employed to perform the necessary action.
file obj = open ( name_of_file [, int_mode ] [, buffering ]
int_mode : This value is basically an integer that represents whether read, write or append action has to be performed on the file. This entire parameter is optional and usually by default it is set to the ‘read’ value.
buffering : Usually by default, the value is set for this as 0. In case, user sets the value of this variable to 1, then line buffering is bound to happen during the process of accessing the file.
Different modes of file opening:
There are different modes or means of opening a file in Python. Let us see in detail what each mode indicates.
- Mode ‘r’: This indicates that a file is ready to open for reading and this is the default mode.
- Mode ‘w’: This indicates that a file is to ready to open for writing and in the case where no file already exists, then a new file gets created.
- Mode ‘x’: This indicates that a file is ready to open for the purpose of protection and ensuring exclusivity from being accessed and altered by other users.
- Mode ‘a’: This indicates that a file is ready to open for appending without truncation.
- Mode ‘t’: This indicates that a file is ready to open in the basic text format.
- Mode ‘b’: This indicates that a file is ready to open in a binary mode.
- Mode ‘+’: This indicates that a file is ready to open for the purpose of updating and making changes.
Writing a file :
For the purpose of making changes and performing ‘write’ operation in a file, Python has the write() function which is used in order to a word or a string of characters. Consider the following code sample which explains how to perform ‘write’ operation in a file.
with open (‘capturelog.txt’, ‘wt’) as f: f.write (‘The initial data is captured in the log \n’) f.write (‘along with all the supporting details. \n’)
The initial data is captured in the along with all the supporting details.
Closing a file:
The syntax of Python also provides the close() function for the purpose of closing a file once all the operations are performed on it. At the juncture of closing the file, the entire system frees up all the memory and resources which are allocated to perform the action so far.
Consider the following syntax for performing the close action.
f = open (“myfile.log”, encoding = ‘utf-8’) f.close()
Process of deletion and renaming:
Now that you are quite familiar with the basic operation of reading, writing, opening and closing a file, the most obvious and helpful next step is to learn about deleting and renaming a file.
The rename() method is used in Python to perform the action. Consider the following example to further understand the working.
import os os.rename (“new_attendance.log”, “old_attendence.log”)
import os os.remove (“new_attendence.log”)