0% found this document useful (0 votes)
2K views3 pages

File Handle Worksheet

The document contains 32 questions about file handling in Python. It covers topics like file processing modes, differentiating between file modes like r+, w+, readline() vs readlines(), advantages of binary vs text files, CSV files, differentiating text files from binary files, reading and writing to files, updating file contents, etc. Students are expected to write Python functions/methods to perform tasks like reading specific lines from a file, finding longest line, counting characters, copying contents between files etc.

Uploaded by

Ak Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views3 pages

File Handle Worksheet

The document contains 32 questions about file handling in Python. It covers topics like file processing modes, differentiating between file modes like r+, w+, readline() vs readlines(), advantages of binary vs text files, CSV files, differentiating text files from binary files, reading and writing to files, updating file contents, etc. Students are expected to write Python functions/methods to perform tasks like reading specific lines from a file, finding longest line, counting characters, copying contents between files etc.

Uploaded by

Ak Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SKD ACADEMY VRINDAVAN

CLASS-XII COMPUTER SCIENCE


FILE HANDLING WORKSHEET

Q1. What are the different file-processing modes supported by Python?


Q2. What is the difference between readline() and readlines() function?

Q3. What is the difference between "w" and "a" modes?

Q4. Differentiate between file modes r+ and w+ with respect to python.

Q5. Differentiate between file modes r+ and rb+ with respect to Python.

Q6. What would be the data type of variable data in the following statements?

(a) data = f.read()


(b) data = f.read(10)
(c) data = f.readline()
(d) data = f.readlines()

Q7. What are the advantages of saving data in:

(i) Binary form


(ii) Text form

Q8. What is a CSV file?

Q9. What are the advantages of CSV file formats?

Q10. Differentiate between a text file and a binary file.

Q11. Write a method in Python to read lines from a text file MYNOTES.TXT and display those
lines which start with the alphabet 'K'.
Q12. Write a function file_long() that accepts a filename and reports the file's longest line.
Q13. Write a program to count the number of uppercase alphabets present in a text file
"Pome.txt".

Q14. Write a function remove_lowercase() that accepts two file names, and copies all lines that
do not  start with a lowercase letter from the first file to the second file.
Q15. Write a program that reads characters from the keyboard one by one. All lower case
characters get stored inside the file LOWER, all upper case characters get stored inside the file
UPPER and all other characters get stored inside file OTHERS.
Q16. Write a statement in Python to perform the following operations:

• To open a text file "MYPET.TXT" in write mode


• To open a text file "MYPET.TXT" in read mode
Q17. Write a method in Python to write multiple lines of text contents into a text file daynote.txt
line.

Q18. Write a Python program to display the size of a file after removing EOL characters, leading
and trailing white spaces and blank lines.
Q19. Write a user-defined function in Python that displays the number of lines starting with 'H' in
the file Para.

txt. Example, if the file contains:


Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
Then the line count should be 2.
Q20. Consider a binary file Employee.dat containing details such as empno: ename: salary
(separator ':'). Write a Python function to display details of those employees who are earning
between 20000 and 40000 (both values inclusive).
Q21. Write a function countmy() in Python to read the text file "DATA.TXT" and count the
number of times "my" occurs in the file.
For example, if the file "DATA.TXT" contains "This is my website. I have displayed my
preferences in the CHOICE section." - the countmy() function should display the output as: "my
occurs 2 times".

Q22. Write a method/function DISPLAYWORDS() in python to read lines from a text file
STORY.TXT, and display those words, which are less than 4 characters.

Q23. Write a method in Python to read lines from a text file DIARY.TXT and display those lines
which start with the alphabet 'P'.

Q24. Consider the file “SarojiniPoem.txt”

Autumn Song

Like a joy on the heart of a sorrow,


The sunset hangs on a cloud;
A golden storm of glittering sheaves,
Of fair and frail and fluttering leaves,
The wild wind blows in a cloud.

Hark to a voice that is calling


To my heart in the voice of the wind:
My heart is weary and sad and alone,
For its dreams like the fluttering leaves have gone,
And why should I stay behind?

Sarojini Naidu

Based on the above file, answer the following question:


 
(a) What would be the output of following code?

file = open ("sarojiniPoem.txt","r")


text = file.readlines()
file.close()
for line in text  :
    print (line , end = ' ' )
print ( )

(b) Modify the program so that the lines are printed in reverse order.

(c) Modify the code so as to output to another file instead of the screen. Let your script overwrite
the output file.

(d)Change the script of part (c) that it append output to an existing file.

(e) Modify the program so that each line is printed with a line number at beginning.

Q25. Write a program using Dictionary and Text files to store roman numbers and find their
equivalents.

Q26. Observe the following code and answer the questions that follow:

File = open ("Mydata","a")


_______#Blank1
File.close()

(i) What type (Text/Binary) of file is Mydata?


(ii) Fill the Blank1 with statement to write "ABC" in the file "Mydata"

Q27. How many file objects would you need to manage the following situations:

(a) To process four files sequentially.


(b) To merge two sorted files into third file.

Q28. Write code to print just the last line of a text file 'data.txt'.

Q29. Write a function in Python to count the number of lines in a text file 'STORY.TXT' which are
starting with the alphabet 'A'.
Q30. A text file contains alphanumeric text (say an.txt). Write a program that reads this text file
and prints only the numbers or digits from the file.

Q31. Following is the structure of each record in a data file named "PRODUCT.DAT".

("prod_code": value, "prod_desc": value, "stock": value)

The values for prod_code and prod_desc are strings and the value for stock is an integer.

Write a function in Python to update the file with a new value of stock. The stock and the
product_code, whose stock is to be updated, are to be inputted during the execution of the
function.

Q32. Create a CSV file "Groceries" to store information of different items existing in a shop. The
information is to be stored w.r.t. each item code, name, price, qty. Write a program to accept the
data from user and store it permanently in CSV file.

You might also like