In Python Programming, for file handling, there is a special function named Python File Open Function. With the help of this function, we can create files, read files, write and append on the files. In all file handling activities, we use Python File Open function.
Python File Open function has two different parameters. These parameters are given below:
The first parameter, filename, shows the file that we will create, read or modify. It is the file that we will do something about it.
The second parameter is the mode that we will use. There are different modes and according to these modes, our file handling action is determined. These modes are given below:
You can reach all Python File Lessons below:
When we use “r” as a second parameter of Python File Open function, this means that we will read the file. So, the file must be an existing file to read it. If it is not existing, then we will receive an error.
If we use “a” or “w” as a second parameter, this means that, we will modify the content. With “a”, we can append new characters, sentences etc. at the end of the file. With “w”, we can overwrite the context, in other words we can change the complete content.
Lastly, if we use “x” mode, this means that we are creating a new file. So, there mustn’t be such a file. If we try to create a file with a filename that is already existing, we will receive and error.
Beside these actions, we can also delete existing files and folders with different python file remove methods.
So, how can we use Python File Open function? Let’s give an example for this. Think about that, there is a file named filexyz.txt in the same folder with Python. We will use this function only to read it. For this, we will use the below code:
Here, the first parameter is the file name and the second parameter is the mode that we will use as action. Here, we will use “r” mode.
We will handle each of the file handling actions in special lessons. But here, let’s give a brief info for each of these activities.
To read an existing file, we use Python File Open function with filename and “r” parameter.
Think about that we have a file named filexyz.txt. To read the content of this file, we will use the below code:
In this example, we have also used read() method of python file object. The return of this Python File Read code will be the content of filzexyz.txt.
To write on an existing file, or to modify it, we use Python File Open function with filename and “w” or “a” parameters. “w” parameter is the completely modify parameter. On the other hand, “a” is the append parameter that adds anything at the end of the content.
Think about that we have a file named fileabc.txt. Its content is like below:
To see the content of this file, we will use the below read code firstly.
The output will be:
Now, we will change the the content of this file with write (w) mode. To do this, we will use the below code:
The output of this Python File Write code and the content of the demo file will be like below:
Now, let’s use append mode to add a new sentence to this existing content.
With this Python File Write code, the new content of this demo file will be like below:
To create a file, we use Python File Open function with filename and “x” parameter.
Think about that we will create a file named Ourfile.txt. To crate this file, we will use the below code:
After this code, we have a file Ourfile.txt.
To remove an existing file, we need to import a module named os module. After that we will use remove method in this module to delete an existing file.
Below, we will delete existing myfile.txt with the help of remove method od os module.
Python File Open Function is the key function for file handling in python. For various reasons, you will work with files during your coding activities. And this function will be the most used function with files.
To work with files on python, you can increase your experience with different python file handling examples. With more examples, you will be more familiar with these methods.
We have seen the summary of these methods here. If you would like to learn more on these methods, you can follow the other python file handling lessons. We will give more and detailed examples in each of these lessons.