Reading Character Data From Text Files

Reading Character Data From Text Files

We can read character data from text file by using the following read methods.
  • read() To read total data from the file 
  • read(n) To read 'n' characters from the file 
  • readline() To read only one line 
  • readlines() To read all lines into a list 

Eg 1: To read total data from the file  

#Follow - Programmerfect

f=open("abc.txt",'r')
data=f.read()
print(data)
f.close()   

Eg 2: To read only first 100 characters: 

#Follow - Programmerfect

f=open("abc.txt",'r')
data=f.read(100)
print(data)
f.close() 


Comments