In any programming language, file handling is an essential part. It reduces the time spent taking input from the user repeatedly and allows you to store the output for later use. In this article, we will provide the basics of file handling in Python and several useful examples that will help you implement file handling.
File Handling in Python
In simple programming, the user provides the input from the console, and the program returns the output on the console. However, there are occasions when simply displaying the input and output on the console is insufficient. For example, the data is huge, and it is not humanly possible to repeatedly input data on the console. At the same time, the output is large and cannot be completely displayed on the console since the memory is volatile. In addition, it is not possible to recover the data, again and again, that was programmatically generated. Here, file handling plays an important role by storing the data permanently in a file.
File handling is relatively difficult in all programming languages. However, it is quite simple and accessible in the Python programming language. Python programming language supports file handling and allows the user to read and write from the file and perform many other operations on the files. It provides several useful and simple built-in functions for creating, modifying, editing, updating, and deleting files.
What Is a Python File IO?
A file is a resource that allows the user to store a piece of data or information in the non-volatile memory of a computer. It is stored in the form of bytes, and Python treats it as either text or binary. The Python file IO is either a text file or a binary file in which each line ends with a special character to indicate the end of the line. In Python programming language, the operations on the file are performed in the following sequence: first, you need to open a file, then perform the required function, and close the file. Almost all file handling processes involve opening and closing files. However, the function may change from time to time.
How Does Python handle File IO?
The Python programming language handles file IO as either text or binary. It provides built-in functions (open() and close()) to open and close specific files respectively.
The open() function opens a file, also known as a handle, that can be used according to the user’s needs, i.e., read, write, edit, etc. It allows the user to provide the file mode in which a user wants to open the file. For example, if a user wants to open a file in write mode, “w” needs to be provided, and if a user wants to open a file in read mode, “r” needs to be provided. The user can also specify in what format he wants to open the file, i.e., text or binary. However, the default is text mode. Below is the list of all the modes in which a file can be opened:
Mode | Description |
---|---|
w | Open a file in write mode. Open an existing file, and if a file does not exist already, a new file will be created and open up for writing. |
r | Open a file in the read mode — the default mode |
x | Open file for creation (exclusive) |
a | Append data in the file |
b | Open file in binary format |
t | Open file in text format — the default format |
rb | Open the file to read-only and in binary format |
wb | Open the file to write only and in binary format |
r+ | Open a file to read and write at the same time |
w+ | Open a file to read and write both while overwriting the previous file |
a+ | Open a file to append and read at the same time |
Now, let’s discuss several examples.
Example 1
In this example program, we will help you learn how to open and close a file. When this code is executed, a text file named “filename” will open in the read mode. It is in the same directory where the code has been executed.
if openfile:
print("The file is opened successfully")
openfile = open("filename.txt","r")
if openfile:
print("The file is opened successfully")
openfile.close()
The previous code gives the following output, and it shows the file is opened successfully:
It is mandatory to close the file which is open in the previous step; to do that, we will use the close() function. The file will be closed after executing the last statement. Here is the following code:
Example 2
First, we need to open the file in write mode, and, to use the write() function, we will write something in the file and then close it. The following statement will open the file (filename.txt) in write mode:
The following statement will write the mentioned data in the file:
Make sure to close the file by using the close function so that the data will not get lost.
The “filename.txt” is now created in the current folder and contains the line “This is a new file, and it is opened in write mode”. You can see this output by simply opening the file by using any text editor, as shown below:
Example 3
Our last example will show you how to append something in our ‘filename’ file. This will open the ‘filename; file in append mode and append the given line at the end of the file.
file.write("Adding a new line in the filename file.")
file.close()
Now, when you try to open the file, here is the following output:
Conclusion
Python provides simple and easy built-in functions to handle files that making file handling quick and easy. In this article, we have learned about different modes of a file in which it can be opened, and with the help of examples, we have learned the implementation of file handling functions.