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

Programs of Text File

The document outlines several programming tasks related to text file manipulation in Python, including counting word occurrences, analyzing letter cases, and filtering lines based on specific criteria. It also includes a code snippet with incomplete statements for creating and displaying city names from a file, along with questions regarding the correct file modes and functions to complete the code. Additionally, it asks for the expected output of the program after execution.

Uploaded by

hemlatamishra049
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)
21 views

Programs of Text File

The document outlines several programming tasks related to text file manipulation in Python, including counting word occurrences, analyzing letter cases, and filtering lines based on specific criteria. It also includes a code snippet with incomplete statements for creating and displaying city names from a file, along with questions regarding the correct file modes and functions to complete the code. Additionally, it asks for the expected output of the program after execution.

Uploaded by

hemlatamishra049
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
You are on page 1/ 2

PROGRAMS OF TEXT FILES

1. WRITE A PROGRAM TO PRINT THE NUMBER OF OCCURRENCE OF WORD “HAPPY” OR “happy” IN THE
GIVEN FILE.
2. WAP TO COUNT THE TOTAL NUMBER OF UPPERCASE, LOWERCASE LETTERS AND DIGITS USED IN THE
TEXT FILE ‘POEM.TXT’
3. WRITE A PYTHON FUNCTION THAT DISPLAYS ALL THE LINES CONTAINING THE WORD ‘vote’ FROM A
TEXT FILE ‘ELECTION.TXT’.
4. WRITE A PYTHON FUNCTION THAT DISPLAYS ALL THE WORDS STARTING AND ENDING WITH A VOWEL
FROM A TEXT FILE ‘REPORT.TXT’. THE CONSECUTIVE WORDS SHOULD BE SEPARATED BY A SPACE IN
THE OUTPUT.
5. Write code for the following:
i) Define a function to create a text file ‘poem.txt’ and stores 5 lines of text in it.
ii) Define a function to accept a list [‘a’,’an’,’the’0 and count the occurrence of each item
of list in the text file ‘poem.txt’ separately.
6. Write a function CAPITALIZE_EACH_WORD() in Python that read the words from file “para.txt” and
copy those words to another file “words.txt” after capitalizing the first letter of each word.

7. Manish created a text file named ‘city.txt’ to store names of 5 cities. He wrote set of statements to
carry out the task but he has doubt in many statements therefore he left blank at those places. Look
carefully the following code and help him to complete the program code.

Def add():
Now
q= open(‘city.txt’,_______) # statement 1
n=[‘Delhi\n’,’Kolkata\n’,’Mumbai\n’,Kanpur\n’,’Dispur\n’]
____________ #statement 2
q.close()
def display():
with open(‘city.txt’) as q:
____________________ # statement 3
for i in r:
G=i.strip()
for ‘a’ not in g:
print(g.capitalize(),end=’@’)
q.close()
add()
display()

Now answer the following questions:


i) Write the suitable mode of file in statement 1.
a) ‘w’ b) ‘wt’ c) ‘r’ d) ‘wb’
ii) To write names of cities, fill the correct statement for statement for statement 2
a) Q.writeline(n)
b) Q.writelines(n)
c) Writelines(n,q)
d) Q.writerow(n)
iii) To read all the cities in a list and store in r in statement 3
a) r=q.read()
b) r=q.readline()
c) r=q.readlines()
d) r=q.readlines(n)
iv) What output of above program code will generate after executing code?
a) DELHI@DISPUR
b) Delhi@Dispur
c) DELHI@DISPUR@
d) Delhi@Dispur@

You might also like