What is file handling?
File handling is a process through which we can create a file, store data in it, retrieve data from the file and also append more information in it.
Does python support file handling?
Like many other languages python also supports file handling.
What is file handling in python?
Python supports file handling and gives us many modes to operate a file. We can create a new file using python code, open existing files, write in files ,read data from files and many more such options. We have many languages which supports file handling , in some languages this topic is a bit complicated, but in python file handling is very easy.
Python treats it's texts and binary differently in file handling.
Before we can start with reading data from a file, or writing some data into it, we need to open a file.
Working of open function for python file handling:
We use open function in python to open a file in different modes , read mode, write mode, etc.
Syntax:
file object = open(file_name,mode)
- file object - This is the object of the file.
- open - open() is the function used to open a file in python.
- file_name - file_name is the name of the file we want to open to edit data or read data from it.
mode - In python we have many modes in which we can operate a file. As python treats texts and binary differently in file handling , so the number of modes is more.
Let us take a look into the different types of modes to operate a file:
Modes in python:
1. r - This is the read mode. In this we can read the informaton the file has. The reading of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
2. rb - This is also a read mode , but here the information is read only in binary format. Here also, the reading of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
3.r+ - This mode is for both reading and writing data. In this we can read data and write data in the same file simultaneously. Here too, the reading and writing of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
4.rb+ -This mode is also for both reading and writing data , but here the information is read and written only in binary format. Here also, the reading and writing of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
5.w - This is the write mode. In this we can write data into the file. If there is already some information in the file , this mode overwrites it. If there is no file already with the specified name, it creates a new file. The writing of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
6.wb -This is also a write mode , but here the information can be written only in binary format. If there is already some information in the file , this mode overwrites it. If there is no file already with the specified name, it creates a new file. The writing of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
7.w+ - This mode is for both reading and writing data. In this we can read data and write data in the same file simultaneously. If there is already some information in the file , this mode overwrites it. If there is no file already with the specified name, it creates a new file. The reading and writing of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
6.wb+ -This mode is also for both reading and writing data , but here the information is read and written only in binary format. If there is already some information in the file , this mode overwrites it. If there is no file already with the specified name, it creates a new file. The writing of the data starts from the beginning of the file, i.e., the file pointer is at the beginning of the file.
7.a - This mode is called append mode. In this we write into the file without overwriting the previous data , i.e., if there is already some information in the file , unlike write mode , append does not overwrites it and it appends more information in the same file. If there is no file already with the specified name, it creates a new file. The writing of the data starts from the end of the file, i.e., the file pointer is at the end of the file.
8.ab - This mode is also append mode, but here the information can be written only in binary format. In this we write into the file without overwriting the previous data , i.e., if there is already some information in the file , unlike write mode , append does not overwrites it and it appends more information in the same file. If there is no file already with the specified name, it creates a new file. The writing of the data starts from the end of the file, i.e., the file pointer is at the end of the file.
9.a+ - This mode is also append mode, but this mode is for both reading and writing data. In this we write into the file without overwriting the previous data , i.e., if there is already some information in the file , unlike write mode , append does not overwrites it and it appends more information in the same file. If there is no file already with the specified name, it creates a new file. The reading and writing of the data starts from the end of the file, i.e., the file pointer is at the end of the file.
10.ab+ - This mode is aldo append mode, but here the information can be written and can be read too , only in binary format. In this we write into the file without overwriting the previous data , i.e., if there is already some information in the file , unlike write mode , append does not overwrites it and it appends more information in the same file. If there is no file already with the specified name, it creates a new file. The reading and writing of the data starts from the end of the file, i.e., the file pointer is at end of the file.
If no mode is specified , python considers 'r' as the default mode.
File object attributes:We saw the different modes in python. Now, once the file is open we can get many information about the file. Let us look some attributes of the file object to get those information:
1.file.mode - It returns the access moe in which the file is opened.
2.file.name - It retuens the name of the file.
3.file.close - It is used to close the file.
4.file.closed - It returns whether the file is open or closed , i.e, it returns True if the file is closed.
5.read([size]) - It reads the specified number of bytes(the size specified) from the file.
6.readline([size]) - It is used to read one entire line from a file.
7.tell() - This returns the current position of file pointer.
8.write(str) - This writes a string into the file.
9.writelines(sequence) - This writes a sequence of strings into the file.
Example 1:
file = open("fname.txt","w")
file.write("Python is a programming language!")
file.close()
Here , a file is created first.
The name of the file is "fname.txt" and it currently in "w" mode ,i.e., write mode. So, the given text is written in the file.
And finally the file is closed.
If we want to open the above file the contents we would see are:
Python is a programming language!
Example 2:
s = open("fname.txt","r+")
print("The read text is ",s)
s.close()
Here, the name of the file is fname , the mode is r+ and we print the read text , finally the file is closed.
Output:
Pytho
Example 3:
fo = open("fname.txt", "r+")
s = fo.read(10)
print "Read String is : ", str
Check current position
position = fo.tell()
print "Current file position : ", position
Reposition pointer at the beginning once again
position = fo.seek(0, 0)
str = fo.read(10)
print "Again read String is : ", str
Close opend file
fo.close()
Read String is : Python is
Current file position : 10
Again read String is : Python is
Example 4:
fo = open("fname.txt", "w")
line="I want internship in DevIncept!"
fo.write(line)
fo.close()
If we open the file , it will have following contents:
I want internship in DevIncept!
This contribution has been made by Prachi Agarwal. Contact me directly on my e-mail:[email protected] . Or ping me at, https://www.instagram.com/prachi_diwan21/ .