Programs on Text File

 

Program 1 : To print the entire file in bytes

Code :

f=open("TextFile.txt","r")
s=f.read()
a=len(s)
print("The size of the file is ",a,"bytes.")

Output :

The size of the file is 125 bytes.

Program 2 : To print number of lines in entire file.

Code :

f=open("TextFile.txt","r")
s=f.readlines()               #Note that we have used readlines() instead of read(). Read "Working with text files" if you don't know the difference. Search it on the above search bar on the page itself.
a=len(s)
print("The number of lines in file is :  ",a)

Output :

The number of lines in file is :  1

Program 3 : Write a program to read a text file line by line and display each word separated by '#'.

Code :

f=open("TextFile.txt","r")
l=f.readlines()
s=''
for i in range(len(l)):
  a=l[i].split()
  for j in a:
    s=s+j
    s=s+'#'
print(s)


Explanation :

Here's a step-by-step explanation of the code:

  1. f=open("TextFile.txt","r"): This line opens the file "TextFile.txt" in read mode. The file pointer is now at the beginning of the file.
  2. l=f.readlines(): This line reads all the lines in the file and stores them in a list called l. Each element in the list is a string representing a line in the file.
  3. s='': This line initializes an empty string s. This string will be used to store all the words from the file, separated by '#' symbols.
  4. for i in range(len(l)): This line starts a loop that iterates over each line in the file. The range(len(l)) function generates a sequence of numbers from 0 to the length of l (exclusive).
  5. a=l[i].split(): This line splits the current line l[i] into a list of words using the split() method. The split() method splits a string into a list where each word is a separate element. In this case, it splits the line into words based on spaces.
  6. for j in a:: This line starts a nested loop that iterates over each word in the current line.
  7. s=s+j: This line appends the current word j to the end of the string s.
  8. s=s+'#': This line appends a '#' symbol to the end of the string s. This symbol is used to separate words in the final output.
  9. print(s): This line prints out the final string s, which contains all the words from the file, separated by '#' symbols

Output :

Hello#there.#My#name#is#Aditya#Kumkar.I#am#going#to#be#an#excellent#engineer,#a#master#problem#solver#and#a#learner#for#life.# 

Program 4 : Write a program to count the number of vowels present in a text file.

Code :

f=open("TextFile.txt","r")
s=f.read()
count=0
for i in s:
  if i=='a' or i=='e' or i=='i' or i=='o' or i=='u':
    count=count+1
print("The number of vowels are : ",count)

Output :

The number of vowels are : 39

Program 5 : Write a program to count number of words in a file.

Code :

f=open("TextFile.txt","r")
s=f.read()
count=0
l=s.split()
for i in l:
  count=count+1
print("The number of words are : ",count)

Output :

The number of words are : 23

Program 6 : Write a program to count the number of times the occurrence of 'is' word in a text file.

Code :

f=open("TextFile.txt","r")
s=f.read()
count=0
l=s.split()
for i in l:
  if i=='is':
    count=count+1
print("The number of words are : ",count)

Output :

The number of words are : 1





Popular posts from this blog