0% found this document useful (0 votes)
35 views2 pages

XII-CS WS5 (CH. 6 File Handling)

Uploaded by

sharma20keshav
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)
35 views2 pages

XII-CS WS5 (CH. 6 File Handling)

Uploaded by

sharma20keshav
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

DELHI PUBLIC SCHOOL, BAREILLY

XII- Computer Science


Worksheet : File Handling in Python

Q 1. Write a program to count the total no. of characters (including spaces) from a file
"book.txt".

Q 2. Write a program to count the number of words from a file "book.txt".

Q 3. Write a program to count the number of characters (without space) from a file
"book.txt".

Q 4. Write a program that prints the contents of a text file on the screen. The name of
text file is to be accepted by the user. Also tell the size of file in bytes.

Q 5. Write a function in Python to count the number of vowels present in a text file
“STORY.TXT”.

Q 6. Write a function in Python to count the number of lowercase alphabets present in


a text file “book.txt”.

Q 7. Write a function in Python to print the count of the word “and” (including “And”,
“AND” too) in the text file “STORY.TXT”.

Q 8. Write a function in Python to read the content of a text file “DELHI.TXT” and
display all those lines on screen, which are either starting with ‘D’ or ‘M’.

Q 9. Write a function DISP_FIVE() in python to read lines from a text file


“STORY.TXT” and display those words, which are less than 5 characters.

Q 10. A binary file “student.dat” has structure [RollNo, Name, class].


a) Write a function AddFile( ) to input data for a record and add it to “student.dat”
file.
b) Write a function CountStuds (class) in Python which accepts a class as a parameter
and count & returns number of students of the given class stored in the binary file
“student.dat”.

Q 11. A binary file “student.dat” has structure (RollNo, Name, Percentage). Write a
function CountNinty( ) in Python that would read contents of the file “student.dat”
and display the details of those students whose percentage is 90 and above. Also
display number of students scoring above 90%.

Q 12. Radha is a programmer, who has recently been given a task to write a python
code to perform the following CSV file operations with the help of two user
defined functions/modules:
a. CSVOpen() : to create a CSV file called BOOKS.CSV in append mode
containing information of books – Title, Author and Price.
b. CSVRead() : to display the records from the CSV file called BOOKS.CSV
where the field title starts with 'R'.
Q 13. Write the definition of a function ChangeGender() in Python, which reads the
contents of a text file "BIOPIC.TXT" and displays the content of the file with every
occurrence of the word 'he' replaced by 'she'.

For example, if the content of the file "BIOPIC.TXT" is as follows :


Last time he went to Agra,
there was too much crowd, which he did not like.
So this time he decided to visit some hill station.

The function should read the file content and display the output as follows :
Last time she went to Agra,
there was too much crowd, which she did not like.
So this time she decided to visit some hill station.

Q 14. Write the definition of a function Count_Line() in Python, which should read
each line of a text file "SHIVAJI.TXT" and count total number of lines present in
text file.
For example, if the content of the file "SHIVAJI.TXT" is as follows :
Shivaji was born in the family of Bhonsle.
He was devoted to his mother Jijabai.
India at that time was under Muslim rule.

The function should read the file content and display the output as follows :
Total number of lines : 3

You might also like