Home >>Python Tutorial >File Handling in Python
File Handling in Python
The Python File Open
This is the file handling in Python module of the entire python tutorial. File handling is an extremely important part of using any particular kind of web application whether it is python or any other programming language. And python thankfully has several different functions that you can use to read, create, update, or even delete a number of different files.
The File Handling
The most important function that you need to be aware of while working with all kinds of files in Python append to file is the open ( ) function. This particular function in this programming language has two different parameters. And those parameters are the mode and the filename. There are further four different types of methods or modes that you can use to open the files that are present in Python. And those methods or modes that you can use to open the various python files are mentioned below.
This “r” stands for reading. This is a default value and you can use this method to open a number of files for reading. A type of error is displayed if the file you are trying to open does not exist. The parameter through which you can complete the task of python append to file is mentioned below.
This “a” stands for append. And with the help of this method, you can open the files that you need to append. While using this method, if you try to open a file that does not exist then it will automatically create that file. This is one of the important points that you should remember in this python file handling tutorial.
This “w” stands for write. And the files that you open with the help of this method allow you to write in those files. This method too creates a file if you are trying to open it and it does not exist.
These “x” stands for creating here. And with the help of this method, you can create specific files. If the file you want to create already exists then an error message will be shown.
In this programming language, you can also specify the kind of file that you would want to handle. This means that you can select whether you want the file to be in binary or a text mode. We have also created a list of the codes in this python file handling tutorial and the list is mentioned below.
“t” : Text – Default value. Text mode.
“b” : Binary – Binary mode (for example, images)
If you wish to open a file in this programming language just for the sake of reading then you can do that by simply specifying the name of the file. You can understand this from the below-mentioned example.
f = open (“demofile.txt”)
The code that is mentioned above is similar to the way in which it is written below.
f = open (“demofile.txt”, “rt”)
This is because of the fact that “r” and “t” are both default values for the text and you are not required to specify those values. You should make sure that the file exists or an error will be displayed.
With this, we finish the file handling in Python part of our entire python tutorial.