0% found this document useful (0 votes)
3 views5 pages

Text File Programs (3 Marks)

The document contains a series of Python programs that perform various text file operations, such as counting vowels, words, and lines based on specific criteria. Each program is presented with its code and a brief description of its functionality. The tasks include reading from files, manipulating strings, and displaying results based on the contents of the files.

Uploaded by

shinyyash2429
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Text File Programs (3 Marks)

The document contains a series of Python programs that perform various text file operations, such as counting vowels, words, and lines based on specific criteria. Each program is presented with its code and a brief description of its functionality. The tasks include reading from files, manipulating strings, and displaying results based on the contents of the files.

Uploaded by

shinyyash2429
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Text File Programs ( 3 marks)

Program1:

Write a python program to count number of vowels in a text file.


fin=open("D:\\[Link]",'r')
str=[Link]()
count=0
for i in str:
if i in “AEIOU” or i in “aeiou”:
count=count+1
print(“Total number of vowels in the given file=”,count)
[Link]( )
Program2:
A text file “Quotes .Txt” has the following data written in it:

Living a life you can be proud of Doing your best Spending your time with people and activities
that are important to you Standing up for things that are right even when it‟s hard Becoming the
best version of you.
Write a user defined function to display the total number of words present in the file.
def countwords( ):
S= open("D:\\[Link]","r")
f=[Link]( )
z=[Link]( )
count=0
for i in z:
count=count+1
print("Total number of words", count)
[Link]( )
countwords( )

Program 3:

Write a function in Python to count the number of lines in a text file „[Link]‟ which is
starting with an alphabet „A‟.

def countlines( ):
file=open(“[Link]”, “r”)
lines= [Link]( )
count=0
for w in lines:
if w[0]== „A‟ or w[0]== „a‟:
count=count+1
print(“Total lines starting with A/a:”, count)
[Link]( )
countlines( )
Program 4:
Write a function in python to count the number lines in a text file
‘[Link]’ which is starting with an alphabet ‘W’ or ‘H’.
If the file contents are as follows:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
The output of the function should be:
W or w : 1
H or h : 2
def count_W_H( ):
f = open (“[Link]”, “r”)
W,H = 0,0
r = [Link]( )
for x in r:
if x[0] == “W” or x[0] == “w”:
W=W+1
elif x[0] == “H” or x[0] == “h”:
H=H+1
[Link]( )
print(“Total number of lines =”,len(r))
print (“W or w :”, W)
print (“H or h :”, H)
count_W_H( )

Program 5:
Write a method in Python to read lines from a text file [Link], 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 great market. Most of the Indians can foresee the heights that
India is capable of reaching.
The output should be 4.

defcount_word( ):
file = open('[Link]','r')
count = 0
for line in file:
words = [Link]( )
for word in words:
if word == 'India':
count += 1
print(count)
[Link]( )
count_word( )
Program 6:

Write a Python program that writes the reverse of each line in “[Link]” to another text file “[Link]”.
Eg: if the content of [Link] is:
The plates will still shift
and the clouds will still spew.
The sun will slowly rise
and the moon will follow too.
The content of “[Link]” should be:
tfihs llits lliw setalp ehT
.weps llits lliw sduolc eht dna
esir ylwols lliw nus ehT
.oot wollof lliw noom eht dna

def reverseline( ):
f=open("[Link]","r")
o=open("[Link]","w")
l=[Link]( )
for i in l:
[Link](i[::-1])
[Link]( )
[Link]( )
reverseline( )
Program 7:

Write and call a Python function to read lines from a text file [Link] and display those lines which
doesn’t start with a vowel (A, E, I, O, U) irrespective of their case.

def display_non_vowel_lines( ):
with open("[Link]", "r") as file:
print("Lines that don't start with a vowel:")
lines = [Link]( )
for line in lines:
if line[0].lower( ) not in 'aeiou':
print(line)
display_non_vowel_lines( )

Program 8:
A) Write a Python function that displays all the words containing @cmail from a text file
"[Link]".
OR
B)Write a Python function that finds and displays all the words longer than 5 characters from a
text file "[Link]".
(A) def show( ):
(B) def display_long_words( ):
f=open("[Link]",'r')
with open("[Link]", 'r') as file:
data=[Link]( )
data=[Link]( )
words=[Link]( )
words=[Link]( )
for word in words:
for word in words:
if '@cmail' in word:
if len(word)>5:
print(word,end=' ')
print(word,end=' ')
[Link]( )
display_long_words( )
show( )

Program 9:
a)

OR
b) Write a Python function that can read a text file and print only numbers stored in the file
on the screen (consider the text file name as "[Link]").
Answer:
a) def c_words( ):
with open("[Link]", "r") as file:
content = [Link]( )
upper_count = 0
lower_count = 0
for char in content:
if [Link]( ):
upper_count += 1
elif [Link]( ):
lower_count += 1
print(f"Uppercase letters: {upper_count}")
print(f"Lowercase letters: {lower_count}")

b) def fn( ):
with open( "[Link]","r") as file:
d=[Link]( )
for x in d:
if [Link]( ):
print(x)
fn( )
Program 10: [Link] a fn. that counts no of words beginning with a capital letter from the text
file [Link]
Example:
If you want to Walk Fast,
Walk Alone.
But - if u want to Walk Far,
Walk Together
Output: No of words starting with capital letter : 10
OR
(B) Write a function that displays the line number along with no of words in it
from the file [Link] Example :
None can destroy iron, but its own rust can!
Likewise, none can destroy a person, but their own mindset can
The only way to win is not be afraid of losing.
Output:
Line Number No of words
Line 1: 9
Line 2: 11
Line 3: 11
Answer: A.
def countCapital( ):
f = open("[Link]","r")
data = [Link]( )
words = [Link]( )
count = 0
for w in words:
if w[0].isupper( ):
count = count + 1
print("No of words starting with capital letter :",count)
[Link]( )
countCapital( )
B. def lineWords( ):
f = open("[Link]","r")
lineNo = 1
for line in f:
words = [Link]( )
count = len(words)
print("Line",lineNo,":",count)
lineNo = lineNo + 1
[Link]( )
lineWords()

You might also like