0% found this document useful (0 votes)
169 views4 pages

Text File Handling

The document contains programming questions and answers related to text file handling in Python for Grade 12 Computer Science students. It includes user-defined functions to perform various tasks such as counting specific words, displaying lines starting with certain letters, copying files while excluding specific lines, and counting vowels and consonants. Each function is provided with a code snippet demonstrating its implementation.

Uploaded by

jkdhanyaa2008
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)
169 views4 pages

Text File Handling

The document contains programming questions and answers related to text file handling in Python for Grade 12 Computer Science students. It includes user-defined functions to perform various tasks such as counting specific words, displaying lines starting with certain letters, copying files while excluding specific lines, and counting vowels and consonants. Each function is provided with a code snippet demonstrating its implementation.

Uploaded by

jkdhanyaa2008
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

SRI AMBAL THULASI PUBLIC SCHOOL SENIOR SECONDARY,

ANNUR-641653
Topic: Text File Handling Programming Questions with answers
Class: Grade 12 Subject: Computer Science

1. Write a user defined function in python that displays the number of lines starting
with 'H' in the file [Link].
def start_H():
fo=open("[Link]","r")
lines=[Link]()
count=0
for line in lines:
if [Link]("H"): # [Link]()[0]=='H'
count+=1
print("number of lines startswith H are:",count)
[Link]()
start_H()

2. Write a function countmy() in python to write the text file "[Link]" and
count the number of times "my" occurs in the file. For example if the file
[Link] contains-"This is my website. I have displayed my preference in the
CHOICE section ".-the countmy() function should display the output as:"my occurs
2 times".
def countmy():
fo=open("[Link]","r")
data=[Link]()
words=[Link]()
count=0
for word in words:
if [Link]()=='my':
count+=1
print("my occurs",count,"times")
[Link]()
countmy()

3. Write a method in python to read lines from a text file [Link] and display
those lines which start with the alphabets P.
def start_P():
fo=open("[Link]","r")
lines=[Link]()
for line in lines:
if [Link]("P"): # [Link]()[0]=='P'
print(line)
[Link]()
start_P()
4. Write a method in python to read lines from a text file [Link] and
display those lines which start with alphabets 'K'.
def start_K():
fo=open("[Link] ","r")
lines=[Link]()
for line in lines:
if [Link]("K"): # [Link]()[0]=='K'
print(line)
[Link]()
start_K()
5. Write a program that copies a text file "[Link]" onto "[Link]" barring the
lines starting with @ sign.
def copy():
fin=open("[Link]","r")
fout=open("[Link]","w")
while True:
line=[Link]()
if len(line)==0:
break
if line[0]=='@':
continue
[Link](line)
print("source file content except @line copied to target file")
[Link]()
[Link]()
copy()

6. Write a function in Python to read a text file, [Link] and displays those lines
which begin with the word ‘You’.
def display_you():
fo=open("[Link]","r")
lines=[Link]()
for line in lines:
words=[Link]()
for word in words:
if [Link]()=='YOU':
print(line)
[Link]()
display_you()

(or)
def display_you():
fo=open("[Link]","r")
lines=[Link]()
for line in lines:
if [Link]().startswith('YOU'):
print(line)
[Link]()
display_you()

7. Write a function, vowelCount() in Python that counts and displays the number of
vowels in the text file named [Link].
def display_vowels():
fo=open("[Link]","r")
data=[Link]()
vc=cc=uc=lc=0
for ch in data:
if ch in 'aeiouAEIOU':
vc+=1
print("vowels count:",vc)
[Link]()
display_vowels()
8. Read a text file line by line and display each word separated by a #.
def wordbyhash():
fo=open("[Link]","r")
data=[Link]()
words=[Link]()
for word in words:
print(word,end='#')
[Link]()
wordbyhash()
9. Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.
def display_vowels():
fo=open("[Link]","r")
data=[Link]()
vc=cc=uc=lc=0
for ch in data:
if [Link]():
uc+=1
if [Link]():
lc+=1
if ch in 'aeiouAEIOU':
vc+=1
elif ch!=' 'and ch!='\n' and ch not in 'aeiouAEIOU':
cc+=1
print("uppercase count:",uc)
print("lowercase count:",lc)
print("Consonant count:",cc)
print("vowels count:",vc)
[Link]()
display_vowels()

10. Remove all the lines that contain the character 'a' in a file and write it to another
file.
def copy():
fin=open("[Link]","r")
fout=open("[Link]","w")
lines=[Link]()
for line in lines:
for ch in line:
if [Link]()=='a':
[Link](line)
break
print("source file content having letter 'a' copied to target file")
[Link]()
[Link]()
copy()
11. Write a function in Python that counts the number of “Me” or “My” words
present in a text file “[Link]”.
If the “[Link]” contents are as follows:
My first book was Me and My Family. It gave me chance to be Known to the world.
The output of the function should be:
Count of Me/My in file: 4

def count_me_my():
fin=open("[Link]","r")
data=[Link]()
words=[Link]()
count=0
for word in words:
if [Link]()=='me' or [Link]()=='my':
count+=1
print("Count of Me/My in file:",count)
[Link]()
count_me_my()

You might also like