0 0
Read Time:3 Minute, 2 Second

Text Files  

  • We can read , write or append data to text files
  • We have to open the text file , apply desired functions and close the file.
  • For Example we have a file abc.txt in our desktop.
  • In Abc.txt file we have some data in three different lines i.e. { For more Educational Updates on programming , web development , Data Science, personality development subscribe to TopperG }

For more Educational Updates on programming
web development , Data Science
Personality development subscribe to TopperG.

Reading Text Files (read() , readline() , readlines())

read()

  • Access / Display complete data of file
# display complete data of file abc.txt

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt")
d = x.read()
print(d)
x.close()

# x is the file object / file handle that have link of text file
# Data is read using read() and saved in d.

OUTPUT : Complete data of file is printed

See also  split() function Important programs in file handling in python

For more Educational Updates on programming
web development , Data Science
Personality development subscribe to TopperG.

# display a particular numbers of characters from file 

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt")
d = x.read(10)
print(d)
x.close()

OUTPUT : d will display only 10 characters

For more E

  • we can also apply file operation mode ‘r’ for reading
# mention file operation mode for reading data, after file address

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d = x.read(10)
print(d)
x.close()

OUTPUT : d will display only 10 characters, we have used ‘r’ mode

For more E

readline()

  • Access / Display a line of file
x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
l1 = x.readline()
l2 = x.readline()
print(l1)
print(l2)
x.close()

OUTPUT : first two lines will be printed via l1 & l2. Because every time a new readline() is executed it will take the next line.

For more Educational Updates on programming
web development , Data Science

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
l1 = x.readline(5)
l2 = x.readline()
print(l1)
print(l2)
x.close()

OUTPUT : first line will be printed via l1 & l2. and l1 will print only first 5 characters of first line, because we have given argument 5 there. and in l2 will get the remaining content of line 1.

See also  working with functions important programs part 1

For m
ore Educational Updates on programming

readlines()

  • create a list in which items are the lines of a text file
x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d2 = x.readlines()
print(d2)
x.close()

OUTPUT : a list is printed of three items, in which each line of abc.txt file is stored as an individual item.

[‘For more Educational Updates on programming\n’, ‘web development , Data Science\n’, ‘Personality development subscribe to TopperG.’]

traversing in the lines of text file

  • access each line of text file individually
x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d1 = x.readlines()
for i in d1:
    print(i)
x.close()

OUTPUT : each line of abc.txt file is printed by loop control variable i.

See also  Reading and Writing CSV Files in Python

For more Educational Updates on programming
web development , Data Science
Personality development subscribe to TopperG.

  • Giving argument in readlines()
x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d1 = x.readlines(60)
print(d1)
x.close()

OUTPUT : All lines will be generated in which 60 characters are present.

[‘For more Educational Updates on programming\n’, ‘web development , Data Science\n’]

Q. Display length of each line written in the file abc.txt.

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d1 = x.readlines()
for i in d1:
    print(len(i))
x.close()

OUTPUT : Length of each line will be printed.

44
31
45

Happy
Happy
%
Sad
Sad
%
Excited
Excited
%
Sleepy
Sleepy
%
Angry
Angry
%
Surprise
Surprise
%