Python open() Function
What Is an open() Function?
We have different files stored in our system. We usually double-click on the file to open it. But when opening a file in a program, we need a function to open it. Python provides built-in functionality to open a file, and it is an open() function. You can use the open() function to open a file, which will return the opened file as a file object that can perform other filing functions.
Syntax of open() Function
The syntax of the open() function is straightforward to understand, and it is given below:
The “file-name” is the file’s name to be opened for a read-and-write operation. The “open-mode” is the mode of the file in which the file will be opened. Four simple modes you can define to open a file are: “r”, “a”, “w”, and “x”. The “r” mode is defined as opening a file to read. It is a default value. By default, the file will open in the read mode, and if a file does not exist, then the function will return an error.
Moreover, the “a” mode is used when the user opens a file for appending the data with already existing data. The “w” mode opens a file for writing purposes. And finally, the “x” mode is used when the user needs to create a specific type of file. In this case, the function will return an error if a file already exists with the same name.
In the first example program, we will see how to open a file that is present in the same directory. A file can exist in the same directory where your Python compiler is running, and it can also exist in some other directory. To open a file in the same directory, you can write the following lines of code:
The “openfile” is a variable that holds the value that the open() function will return. The ‘TestFile.txt’ is the name of the file which will be opened by the open() function in text mode. Since no mode has been specified, the file will open in read-only and text mode by default. When you run this code, you will get to see this kind of file opened on your screen:
Now that we have learned how to open a file in the same directory, let us see how to open a file in a different directory. When you try to open a file in a different directory, you need to route specifically to that directory by providing the complete path. You need to use the following line of code to open a file in a different directory:
Here, the path says, “C” is the main directory of the system, “Kalsoom” is the subdirectory or the system’s username, and “TextFile.txt” is the name of the file. This is the complete path to the file that needs to be opened in text mode from the “C” directory. After that, we used the print statement where the read() function is used to open the file whose path is specified in the above line. When you run this line of code, you will get to see the same file opened on the screen that you have seen in the previous example:
As we have discussed, by default, a file is open in a read mode. If a user wants to open a file in a different mode, then the mode needs to be specifically defined. In this example, we will specify how to define the mode of the file. Here, we are opening a specified file in write mode. The following line of code is used to open a file in write mode.
As we have already defined, the ‘w’ is used to define the write mode. So, we used ‘w’ in the open() function with the “mode” argument to inform the compiler that the file needs to be opened in a write mode. When the open() function opens the TestFile, the compiler allows you to write data in it.
Since we opened the file in write mode, you’ll notice that the data has been overwritten. If we open the file in an append mode, then the data will be appended to the file.
In this example, we’ll talk about how to use the “with” statement in the open function. To open a file, the with statement collaborates with the open() method. Therefore, you can modify the code we used in the example of the open() function as follows:
Below you can see the output:
Unlike open(), which asks you to close the file with the close() method, the with statement automatically closes the file. This is due to the with statement indirectly using the built-in functions __enter() and __exit(). When the operation you specify is finished, the __exit()__ method closes the file.
This tutorial is a quick guide about the open() function in file handling. File handling has many options that can be performed on a file, i.e., open a file, read the data in the file, write something in the file, append data in a file, etc. However, this post specifically talked about the open() function. The open() function opens a file in a text and read mode, which are the default values. To open a file in binary or any other mode, it needs to be specifically defined.