Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
1. Count number of ‘S’ or ‘T’ (both) from a text file.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
#print(content)
s=0
t=0
for i in content:
if i in 'Ss':
s += 1
elif i in 'Tt':
t += 1
print('S:', s)
print('T:', t)
print(s+t)
[Link]()
textFile()
2. Count number of Vowels, Consonants, Spaces, Digits and Special characters
from a text file.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
vowel = 0
consonant = 0
space = 0
digit = 0
special = 0
n=0
for i in content:
if [Link]():
if i in 'aeiouAEIOU':
vowel += 1
else:
consonant += 1
elif i == ' ':
space += 1
elif [Link]():
digit += 1
elif i=='\n':
n+=1
else:
special += 1
print('Vowel:', vowel)
print('Consonant:', consonant)
print('Space:', space)
print('Digit:', digit)
print('Special:',special)
print('Line:', n)
[Link]()
textFile()
3. Print the words that begin vowel
def textFile():
f = open('[Link]', 'r')
content = [Link]()
1
Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
words = [Link]()
for i in words:
if i[0] in 'aeiouAEIOU':
print(i)
[Link]()
textFile()
4. Print the words that begin the H or R
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
if i[0] in 'HhRr':
print(i)
[Link]()
textFile()
5. Print the words that begin and end with the same characters.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
#if len(i)>1:
if i[0] == i[-1]:
print(i)
[Link]()
textFile()
6. Print the words that begin and end with consonant.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
if i[0].isalpha() and i[-1].isalpha():
if i[0] not in 'aeiouAEIOU' and i[-1] not in 'aeiouAEIOU':
print(i)
[Link]()
textFile()
7. Print the words that end with a digit
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
if i[-1].isdigit():
print(i)
[Link]()
textFile()
8. Print the three lettered words from a file
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
2
Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
if len(i) == 3:
print(i)
[Link]()
textFile()
9. Print the word that has the letter e in it.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
if 'e' in i or 'E' in i:
print(i)
[Link]()
textFile()
10. Print the word that has more than 5 characters in it.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
if len(i) > 5:
print(i)
[Link]()
textFile()
11. Print the words that has more than 2 ‘t’ letters in it.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
a=[Link]()
if [Link]('t') > 2:
print(i)
[Link]()
textFile()
12. Count the number of words present in the file.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
print("No of words in the file:", len(words))
[Link]()
textFile()
13. Count the words that begin the K or ‘k’
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
if i[0] in 'kK':
print(i)
[Link]()
textFile()
14. Count the number of ‘this’ present in a file. (THIS, THis, tHIS, all possible
3
Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
combination to be included)
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
this = 0
for i in words:
if 'this' in [Link]():
this += 1
print("No of this words :", this)
[Link]()
textFile()
15. Print the lines that begin with the letter T or W.
def textFile():
f = open('[Link]', 'r')
for line in [Link]():
if line[0] in 'TW':
print(line, end = '')
[Link]()
textFile()
16. Print the lines that begin and end with same letter.
def textFile():
f = open('[Link]', 'r')
#print(f)
for line in f:
if line[0] == line[-2]:
print(line, end = '')
[Link]()
textFile()
17. Print the lines that begin with the word ‘The’. (‘THE’, ‘THe’, ‘ThE’, all possible
combination to be included)
def textFile():
f = open('[Link]', 'r')
for line in f:
w = [Link]()
if w[0].lower() in 'the':
print(line, end = '')
[Link]()
textFile()
18. Print the lines that has more than 10 words in it.
def textFile():
f = open('[Link]', 'r')
for line in f:
w = [Link]()
if len(w) > 10:
print(line, end = '')
[Link]()
textFile()
19. Print the lines that has the characters ‘he’ in it. (‘he’ can be a separate word or
can be a part of word)
def textFile():
f = open('[Link]', 'r')
for line in f:
if 'he' in line:
4
Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
print(line, end = '')
[Link]()
textFile()
20. Print the lines that has the word ‘from’ in it.(from, FROM, FroM, all possible
combination to be included)
def textFile():
f = open('[Link]', 'r')
for line in f:
l = [Link]()
words = [Link]()
if 'FROM' in words:
print(line, end = '')
[Link]()
textFile()
21. Count the number of lines present in a file.
def textFile():
f = open('[Link]', 'r')
lines = [Link]()
print("No of lines in the file:", len(lines))
[Link]()
textFile()
22. Count the number of characters present in each line of the file.
#Output:
#Line 1: 65
#Line 2: 42
#Line 3: 55
def textFile():
f = open('[Link]', 'r')
lineNumber = 1
for line in f:
print("Line",lineNumber,':',len(line))
lineNumber +=1
[Link]()
textFile()
23. Count the number of words present in each line of the file.
#Output:
#Line 1: 12
#Line 2: 8
#Line 3: 11
def textFile():
f = open('[Link]', 'r')
lineNumber = 1
for line in f:
words = [Link]()
print("Line",lineNumber,':',len(words))
lineNumber +=1
[Link]()
textFile()
24. Count the number of ‘e’ present in each line.
def textFile():
f = open('[Link]', 'r')
lineNumber = 1
for line in f:
print("Line",lineNumber,':',[Link]('e'))
5
Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
lineNumber +=1
[Link]()
textFile()
25. Count the number of ‘the’ words present in each line.
def textFile():
f = open('[Link]', 'r')
lineNumber = 1
for line in f:
l = [Link]()
words = [Link]()
print("Line",lineNumber,':',[Link]('THE'))
lineNumber +=1
[Link]()
textFile()
26. Print the words that doesn’t have any vowels in it.
def textFile():
f = open('[Link]', 'r')
content = [Link]()
words = [Link]()
for i in words:
w = [Link]()
if not('A' in w or 'E' in w or 'I' in w or 'O' in w or 'U' in w):
print(i)
[Link]()
textFile()
27. Print the lines that has more than 2 ‘is’ word in it.
def textFile():
f = open('[Link]', 'r')
lineNumber = 1
for line in f:
l = [Link]()
words = [Link]()
if [Link]('IS') > 2:
print(line)
[Link]()
textFile()
28. Print the line that has more than 3 four lettered words.
def textFile():
f = open('[Link]', 'r')
for line in f:
words = [Link]()
c=0
for w in words:
if len(w) == 4:
c += 1
if c > 3:
print(line)
[Link]()
textFile()
29. Copy the lines that has the word ‘that’ in it to a new file named “[Link]’
def textFile():
f1 = open('[Link]', 'r')
f2 = open('[Link]','a')
for line in f1:
6
Class – XII Chapter – 4 (File Handling – Text File Programs)Computer Science - 083
l = [Link]()
words = [Link]()
if 'THAT' in words:
[Link](line)
[Link]()
[Link]()
textFile()
30. Prints the words starting with 'O' in reverse order. The rest of the content is
displayed normally.
def textFile():
f = open('[Link]', 'r')
for line in f:
words = [Link]()
for w in words:
if w[0].lower()=='o':
print(w[::-1],end=" ")
else:
print(w,end=' ')
print()
[Link]()
textFile()
31. Display those lines, which have '@' anywhere in the line.
def textFile():
f = open('[Link]', 'r')
for line in [Link]():
if '@' in line:
print(line,end='')
[Link]()
textFile()