Various properties of the file object:
Once open() is successful and file object gets created, we can retrieve various details related to that file using these associated properties :
Name: Name of the opened file
Mode: mode in which the file gets opened
Closed: returns a Boolean value, (True/False) which indicates whether the file is closed or not.
readable: returns a Boolean value, (True/False) which indicates whether the file is readable or not.
f=open("test","r") print("File name : ",f.name) print("File mode : ",f.mode) print("Is File readable : ",f.readable()) print("Is File closed : ",f.closed); #close the file f.close()
Output:
File name : test File mode : r Is File readable : True Is File closed : False >>>
Reading from a file
Python provides various methods for reading data from a file. we can read character data from a text file using the following read methods:
1. read() :
To read the entire data from the file. Starts reading from the cursor up to the end of the file.
2. read(n):
To read “n” characters from the file. Starting from the cursor, if the file has fewer characters than “n”, it will read until the end of the file.
3. readline():
To read only one line from the file; starts reading from the cursor up to and including the end of line character.
4. readlines():
To read all the lines from the file into a list; starts reading from the cursor up to the end of the file and return a list of lines.
For the examples below, we have used a file “test.txt”
File: test.txt contains
This is line1
This is line2
This is line3
This is line4
This is line5
This is line6
This is line7
This is line8
This is line9
This is line10
Example read():
We have a file “test.txt” and we want to read the contents of the file using read() method.
#to read the contents of the file f=open("test.txt","r") data=f.read() print(data) #close the file f.close()
Output:
This is line1 This is line2 This is line3 This is line4 This is line5 This is line6 This is line7 This is line8 This is line9 This is line10 >>>
Example read(n):
We have a file “test.txt” and we want to read 1st 7 characters from the file using read(n) method.
#to read the contents of the file f=open("test.txt","r") data=f.read(7) print(data) #close the file f.close()
Output:
This is >>>
We have a file “test.txt” and we want to read 1st 7 characters from the file using read(n) method.
#to read the contents of the file f=open("test.txt","r") data=f.read(13) print(data) #close the file f.close()
Output:
This is line1 >>>
Example readline():
We have a file “test.txt” and we want to read 1st 3 lines from the file using read(n) method.
#to read the contents of the file f=open("test.txt","r") data=f.readline() print(data) data=f.readline() print(data) data=f.readline() print(data) #close the file f.close()
Output:
This is line1 This is line2 This is line3 >>>
Example readlines():
We have a file “test.txt” and we want to read contents from the file using readlines() method.
#to read the contents of the file f=open("test.txt","r") data=f.readlines() print(data) #close the file f.close()
Output:
Since the output is stored in the form of list, so we get the following result.
['This is line1\n', 'This is line2\n', 'This is line3\n', 'This is line4\n', 'This is line5\n', 'This is line6\n', 'This is line7\n', 'This is line8\n', 'This is line9\n', 'This is line10'] >>>
To read the contents and display in proper each line in proper format we modified above program as:
#to read the contents of the file f=open("test.txt","r") data=f.readlines() for i in data: print(i) #close the file f.close()
Output:
This is line1 This is line2 This is line3 This is line4 This is line5 This is line6 This is line7 This is line8 This is line9 This is line10 >>>
Modified Above Program
#to read the contents of the file f=open("test.txt","r") data=f.readlines() for i in data: print(i,end='') #close the file f.close()
Output:
This is line1 This is line2 This is line3 This is line4 This is line5 This is line6 This is line7 This is line8 This is line9 This is line10 >>>