0% found this document useful (0 votes)
97 views

PROGRAMS

The document provides examples of Python functions/methods to perform various operations on text files like counting words, displaying lines based on conditions, writing to files, etc. Specifically, it shows: 1. A function to count occurrences of words like "IS", "TO", "UP" in a text file. 2. Functions to display lines from a file starting with 'A' or 'E' and lines starting 'A' or 'B'. 3. A function to display words less than 5 characters from a file. 4. A function to count occurrences of a word in a file. 5. Functions to display lines starting with particular alphabets from a file. 6. Functions to write
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

PROGRAMS

The document provides examples of Python functions/methods to perform various operations on text files like counting words, displaying lines based on conditions, writing to files, etc. Specifically, it shows: 1. A function to count occurrences of words like "IS", "TO", "UP" in a text file. 2. Functions to display lines from a file starting with 'A' or 'E' and lines starting 'A' or 'B'. 3. A function to display words less than 5 characters from a file. 4. A function to count occurrences of a word in a file. 5. Functions to display lines starting with particular alphabets from a file. 6. Functions to write
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

I.

Write a method/function ISTOUPCOUNT() in python to read


contents from a text file WRITER.TXT, to count and display the
occurrence of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’.
For example :
If the content of the file is
IT IS UP TO US TO TAKE CARE OF OUR SURROUNDING.
IT IS NOT POSSIBLE ONLY FOR THE GOVERNMENT TO TAKE
RESPONSIBILITY
The method/function should display
Count of IS TO and UP is 6

def ISTOUPCOUNT():
file=open('WRITER.TXT','r')
line = file.read()
word = line.split()
cnt=0
for w in word:
if w=='TO' or w=='UP' or w=='IS':
cnt+=1
print("Count of IS, TO and UP is:",cnt)
file.close()
ISTOUPCOUNT()

II. Write a method/function AEDISP() in python to read lines from a


text file WRITER.TXT, and display those lines, which are starting
either with A or starting with E.
For example :
If the content of the file is
A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD
HEALTH.
WE SHOULD TAKE CARE OF OUR ENVIRONMENT.
EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.
The method should display
A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD
HEALTH.
EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.

def AEDISP():
file=open('WRITER.TXT','r')
lines = file.readlines()
for w in lines:
if w[0]=="A" or w[0]=="E":
print(w)
file.close()
AEDISP()

III. Write a method/function ABLINES() in python to read contents


from a text file LINES.TXT, to display those lines, which are either
starting with an alphabet ‘A’ or starting with alphabet ‘B’. For
example: If the content of the file is
A BOY IS PLAYING OUTSIDE THE PLAYGROUND IS BIG
BANYAN TREE IS IN THE GROUND
The method/function should display
A BOY IS PLAYING OUTSIDE BANYAN TREE IS IN THE
GROUND
def ABLINES():
file=open('LINES.TXT','r')
lines = file.readlines()
for w in lines:
if w[0]=="A" or w[0]=="B":
print(w)
file.close()
ABLINES()
IV. Write a method/function SHORTWORDS() in python to read lines
from a text file WORDBANK.TXT, and display those words, which
are lesser than 5 characters.
For example: If the content of the file is
HAPPY JOY WELCOME KITE LOVELY POSITIVE FUN
The method/function should display
JOY
KITE
FUN
def SHORTWORDS():
file=open('WORDBANK.TXT','r')
line = file.read()
word = line.split()
for w in word:
if len(w)<5:
print(w)
file.close()
SHORTWORDS()

V. Write a method in Python to read lines from a text file INDIA.TXT,


to find and display the occurrence of the word 'India'. For example,
if the content of the file is:
India is the fastest-growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is capable of
reaching.
The output should be 4.

def display1():
count = 0
file = open("INDIA.TXT","r")
for LINE in file:
Words = LINE.split()
for W in Words:
if W == "India":
count = count + 1
print (count)
file.close()
display1()
VI.  Write a method in Python to read lines from a text file
MYNOTES.TXT and display those lines which start with the
alphabet 'K'.
def display () :
file = open ('lines.txt', 'r')
line = file.readline ()
while line :
if line [ 0 ] == 'K' :
print (line)
line = file.readline ()
file.close()
display()
VII. Write a method in python to read lines from a text file DIARY.TXT
and display those lines which start with the alphabets P.
def display():
file = open ('lines.txt', 'r')
line = file.readline ()
while line :
if line[0] == 'P' :
print (line)
line = file.readline ()
file.close()
display()

VIII. Write a method in python to write multiple line of text contents into
a text file mylife.txt line.
def write () :
f = open (mylife.txt", 'w')
while True:
line = input ("Enter line:")
f.write (line)
choice = input("Are there more lines (Y/N):")
if choice == 'N':
break
f.close()
write()
IX. Write a function in python to read the content from a text file
"poem.txt" line by line and display the same on screen.
def read_file():
f1 = open(".TXT ",'r')
data = f1.readlines()
for i in data :
print(i)
f1.close()

You might also like