You are on page 1of 12

Class 12 – Computer Science

Chapter 5 – File Handling


Article.txt

COUNTLINES – READLINES
Function Definition
f=open()
r=f.readlines()
for() – Takes a sentence
if – use Index Values – Takes a single Character
else/elif
f.close()
print output
Function Call

1
1. Write a function in python to count the number lines in a text file ‘Article.txt’ which is starting with an
alphabet ‘W’ or ‘H’.

2. Write a function in python to count the number lines in a text file ‘Article.txt’ which is starting with an
alphabet ‘G’.

3. Write a Python program to find the number of lines in a text file ‘Article.txt’.

2
DISPLAY LINES – READLINES
Function Definition
f=open()
r=f.readlines()
for() – Takes a sentence
if – use Index Values – Takes a single Character
else/elif
f.close()
print output
Function Call
4. Write a method/function SHOW_TODO() in python to read contents from a text file Article.TXT and display
those lines which have occurrence of the word ‘‘TO’’ or ‘‘DO’’.

5. Write a function in python that displays the lines starting with ‘H’ in the file “Article.txt”.

3
COUNT WORDS – READ
Function Definition
f=open()
r=f.read()
s=r.split()
for() – Takes a word
if
else/elif
f.close()
print output
Function Call

6. Write a user defined function countwords() to display the total number of words present in the file from a
text file “Article.Txt”.

7. Write a function COUNT_AND( ) in Python to read the text file “Article.TXT” and count the number of times
“AND” occurs in the file. (include AND/and/And in the counting)

4
8. Write a function that counts and display the number of 5 letter words in a text file “Article”.txt

9. Write a function in Python that counts the number of “Me” or “My” words present in a text file
“Article.TXT”.

10. Write a code in Python that counts the number of “The” or “This” words present in a text file
“Article.TXT”.

5
COUNT CHARACTER – READ
Function Definition
f=open()
r=f.read()
for() – Takes a Character(Byte/Byte)
if - use Index Values – Takes a single Character
else/elif
f.close()
print output
Function Call
11. Write a function AMCount() in Python, which should read each character of a text file
Article.TXT, should count and display the occurrences of alphabets A and M (including small
cases a and m too).

6
12. Write a function VowelCount() in Python, which should read each character of a text file Article.TXT,
should count and display the occurrence of alphabets vowels.

DISPLAY WORDS – READ


Function Definition
f=open()
r=f.read()
s=r.split()
for() – Takes a word
if – use Index Values – Takes a single Character
else/elif
f.close()
print output
Function Call

7
13. Write a function DISPLAYWORDS( ) in python to display the words starting with “t” or “T” in a text
file ‘ARTICLE.TXT’.

14. Write a function in python to read lines from file “Article.txt” and display all those words, which
has two characters in it.

8
15. Write a function filter(oldfile, newfile) that copies all the lines of a text file “Article.txt” into
“Article1.txt” except those lines which starts with “@” sign.

16. Consider the following definition of dictionary Member, write a method in python to write the
content in a pickled file member.dat.
Member = {'MemberNo.': _____, 'Name': _____}

9
17. Consider the following definition of dictionary Staff, write a method in python to search and display
the content in a pickled file staff.dat, where Staff code key of the dictionary is matching with 'S0103'.
Staff = {‘Staff Code': _____, 'Name' = _____}
CODING

OUTPUT

Staff.dat

10
18. (a) ADD() – To accept and add data of an employee to a CSV file ‘Employee.csv” that has structure (Employee
_No, Employee _Name, Salary)
(b) COUNT() – To count the number of records present in the CSV file named ‘Employee.csv

19. Bookdetails() – To accept and add data of a BOOK to a CSV file ‘BOOK.csv’. Each record consists of a
list with field elements as BOOK NAME and PRICE to store Book name and Price
respectively.
(b) Find()- To display the records of the Book whose price is equal to 750

11
20. Write a python program that defines and calls the following user defined functions.
ADDBOOK() – To read the details of the books from the user and write into the csv file named “book.csv”,The
details of the books stored in a list are bookno,name,author
SEARCHBOOK() – To read the author name from the user and display the books written by the author

12

You might also like