CBSE Class 12 Computer Science File Handling Worksheet Set A

Access the latest CBSE Class 12 Computer Science File Handling Worksheet Set A. We have provided free printable Class 12 Computer Science worksheets in PDF format, specifically designed for File Handling. These practice sets are prepared by expert teachers following the 2025-26 syllabus and exam patterns issued by CBSE, NCERT, and KVS.

File Handling Computer Science Practice Worksheet for Class 12

Students should use these Class 12 Computer Science chapter-wise worksheets for daily practice to improve their conceptual understanding. This detailed test papers include important questions and solutions for File Handling, to help you prepare for school tests and final examination. Regular practice of these Class 12 Computer Science questions will help improve your problem-solving speed and exam accuracy for the 2026 session.

Download Class 12 Computer Science File Handling Worksheet PDF

1. Data File Data files are the files that store and preserve data released to a particular application.
Computers store every file as a collection of 0s and 1s, i.e. in binary form. Therefore, every file is basically just a series of bytes stored.

2. Text File It is a sequence of characters consisting of alphabets, numbers and other special symbols.
It stores information in ASCII characters. In text file, each line of text is terminated with a special character known as EOL (End of Line) or delimiter.

The file extension of text file is .txt.

(i) Opening a File In file handling, the first requirement is opening a file. Once a file is opened, read, write, append, close, etc., can be performed on the file. To open a file, open() method is used in Python.

(ii) Closing a File It is important to close files as soon as you have finished your work with file.
Opened file is closed by calling the close() method of its file objects.

(iii) Writing into Files For writing to a file, we first need to open it in write or append mode. In Python, there are two methods of file objects which are used to write data into a files.

• write() This method takes a string and writes it into the file. This method does not add a newline character (‘\n’) to the end of the string.
Syntax <FileHandle>.write(str1)

• writelines () Whenever, we have to write a sequence of string to a file, we will use writelines() instead of write().
Syntax <FileHandle>.writelines(L)

(iv) Reading from Files After writing information into a file, you must need to read that data at some point. Python provides mainly four types of read methods to read data from file.
• read() This method returns a string containing all characters in a file.
Syntax <FileHandle>.read()

• read([size]) This method specifies how many characters the string should return.
Syntax <FileHandle>.read(n)

• readline() This method will read from a file line by line. For readline(), a line is terminated by ‘\n’ or End of Line (EOL) (i.e. new line character). When end of file is reached, readline() will return an empty string.
Syntax <FileHandle>.readline()

• readlines() This method will return a list of strings, each separated by ‘\n’.
This method reads all rows and retains the newlines character that is at the end of every row.
Syntax <FileHandle>.readlines()

(v) flush() Method This method will force out any unsaved data that exists in a program buffer to the actual file.
Python automatically flushes the files when closing them. But you may want to flush the data before closing any file.
Syntax FileObject.flush()

(vi) Random Access Methods Python provides two methods to perform random access operations in a file. These methods help you to manipulate the position of file pointer and thus you can read and write from desired position in the file.

• tell() Method This method tells you the current position within the file, measured in bytes from the beginning of the file.
Syntax File0bject.tell()

•seek() Method This method can be used to change the current position in a file. This method does not return any value.
Syntax File0bject.seek(offset[,mode])

3. Binary File It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here. They represent the actual content such as image, audio, video, executable files, etc. These files are not human readable. The file extension of binary file is .dat.

(i) Working with Binary File Python object handles the binary files. Python provides a special module-the pickle module which can be used to store any kind of object in file.

(ii) Using dump( ) Method This method is used to write objects to a file. Before use the dump( ) method, you first have to import the pickle module.
Syntax
import pickle
......
......
......
pickle.dump(object_to_pickle,File0bject)

(iii) Using load() Method
This method is used to load data from a binary file.
Syntax
import pickle
......
......
......
object=pickle.load(File0bject)

4. CSV File CSV (Comma Separated Values) format is one of the most simple and common ways to store data in tabular form. Each record consists of one or more fields separated by commas. To represent a CSV file, it must be saved with the .csv file extension.

(i) Working with csv file csv files are used to store a large number of variables or data. They are
incredibly simplified spreadsheets. Each line in a csv file is a data record.

(ii) Read from CSV File Using csv.reader() To read data from csv files, you must use the reader() function to generate a reader object.
This function returns a reader object which is an iterator of lines in the csv file.
Syntax csv.reader(<FileHandle>)

(iii) Write into a CSV File Using csv.writerow() To write an existing file, you must first open the file in one of writing modes (w, a or r+) first. For this, writerow() function is used in Python for csv files.
This function writes items in a sequence (list, tuple or string) separating them by comma character.

 

Direction (Q. Nos. 1-15) Each of the question has four options out of which only one is correct.

Select the correct option as your answer.

1. To read three characters from a file object f, we use ……… .
(a) f.read(3)
(b) f.read()
(c) f.readline()
(d) f.readlines()
Answer : A

2. How do you get the current position within the file?
(a) fp.seek()
(b) fp.tell()
(c) fp.loc()
(d) fp.pos()
Answer : B

3. The files that consists of human readable characters
(a) binary file
(b) text file
(c) Both (a) and (b)
(d) None of these
Answer : B

4. Which function is used to write a list of string in a file?
(a) writeline()
(b) writelines()
(c) writestatement()
(d) writefullline()
Answer : A

5. What will be the output of the following Python code?
myFile = None
for i in range (8):
with open(“data.txt”, “w”) as myFile:
if i > 5:
break
print(myFile.closed)
(a) True
(b) False
(c) None
(d) Error
Answer : A

6. What is the use of seek() method in files?
(a) Sets the file’s current position at the offset
(b) Sets the file’s previous position at the offset
(c) Sets the file’s current position within the file
(d) None of the mentioned
Answer : A

7. What will be the output of the following Python code?
myFile = open(“story.txt”, “wb”)
print(“ Name of the file: ”, myFile.name)
myFile.flush()
myFile.close()
(a) Compilation error
(b) Runtime error
(c) No output
(d) Flushes the file when closing them
Answer : D

Suppose the content of the file ‘‘story.txt’’ is
Education Hub
Learning is the key of success.

8. What is the output of following code?
myfile=open(‘story.txt’,‘r’)
s=myfile.read(10)
print(s)
s1=myfile.read(15)
print(s1)
myfile.close()
(a) Education
Hub Learning is
(b) Education
Hub
Learning is
(c) Education 
Hub
Learning
is
(d) Education Hub
Learning is
Answer : B

9. What is the output of following code?
f=open(‘story.txt’, ‘r’)
s=f.readline()
lcount=len(s)
print(lcount)
f.close( )
(a) 4
(b) 2
(c) 1
(d) 8
Answer : B

10. What is the output of following code?
f=open(‘story.txt’, ‘r’)
str=“ ”
s=0
ts=0
while str:
     str=f.readline()
     ts=ts+len(str)
print(ts)
f.close()
(a) 44
(b) 43
(c) 37
(d) 45
Answer : A

11. What is the output of following code?
def test () :
s=open (“story.txt”, “r”)
f=s.read()
z=f.split( )
count=0
for i in z:
     count=count+1
print(count)
(a) 7
(b) 8
(c) 5
(d) 9
Answer : B

Suppose the content of file ‘‘Para.txt’’ is
Education Hub
Electronic learning
Learning is the key of success

12. What is the output of following code?
def test() :
f=open (“Para.txt”, “r”)
lines=0
l=f.readlines( )
for i in l:
     if i [0] = = ‘E’ :
          lines+=1
print(lines)
(a) 2
(b) 1
(c) 3
(d) Error
Answer : A

13. What is the output of following code?
def myFunc() :
f=open (“Para.txt”, “r”)
count=0
x=f.read()
word=x.split()
for i in word:
     if (i!=“learning”):
          count=count+1
     print(count)
myFunc()
(a) 2
(b) 10
(c) 8
(d) Error
Answer : C

14. What is the output of following code?
def test():
f=open(“Para.TXT”)
n1=0
n2=0
while True:
     l=f.readline()
     if not l:
          break
     for i in l:
          if (i==‘E’ or i==‘e’):
               n1=n1+1
          elif(i==‘C’ or i==‘c’) :
               n2=n2+1
print(n1)
print(n2)
f.close()
(a) 8
     5
(b) 8
      4
(c) 3
     7
(d) Error
Answer : A

15. What is the output of following code?
def Func():
     with open(‘Para.txt’, ‘r’) as f :
          l=f.readlines()
f.close( )
print(l)
del l[3]
print (l)
f=open (‘Para.txt’, ‘w’)
f.writelines(l)
f.close( )
(a) Delete the 3rd word from file
(b) Delete the 4th word from file
(c) Delete the 3rd word from file at end
(d) Error
Answer : B

 
 
1. To open a file c:\scores.txt for reading, we use
a) Infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)
 
2. To open a file c:\scores.txt for writing, we use
a) outfile = open(“c:\scores.txt”, “w”)
b) outfile = open(“c:\\scores.txt”, “w”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)
 
3. What is a file? What are the two basic operations which are performed on a file?
 
4. What is the difference between a text file and a binary file?
 
5. Differentiate between ‘write’ mode and ‘append’ mode.
 
6. What different functions are available to write data in a text file?Describe each in brief.
 
7. What different functions are available to read data from a text file? Describe each in brief.
 
8. Why is it important to close a file?
 
9. In which mode should a file be opened:
a) To write data into an existing file.
b) To read data from a file
c) To create a new file and write data into it.
d) To write data into an existing file. If the file does not exist, then file should be created to write data into it.
 
10. Differentiate between seek() and tell().
 
11. Which functions are used to (i) rename a file, (ii) delete a file? In which module are these functions available?
 
12. What are the three standard input-output streams in Python?
 
13. Define the terms ‘pickling’ and ‘unpickling’.
 
14. What are different file opening modes for a text file in Python? Write,in brief, what does each mode do?
 
15. Consider the following code:
ch = "A"
f=open("data.txt",'a')
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution of the program is the string "ABC"? (Note that" " are not the part of the string)
 
16. Write a single statement to display the contents of a text file named "abc.txt"
 
17. Assuming that a text file named TEXT1.TXT already contains some text written into it, write a function named vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT,
which shall contain only those words from the file TEXT1.TXT which
don’t start with an uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’, ‘U’).
For example, if the file TEXT1.TXT contains
Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain Carry and When rains
 
18. Write a function in PYTHON to count and display the number of
words starting with alphabet ‘A’ or ‘a’ present in a text file
“LINES.TXT”. Example:
If the file “LINES.TXT” contains the following lines,
A boy is playing there. There is a playground.
An aeroplane is in the sky.
Are you getting it?
The function should display the output as 5.
 
19. Write a function show(n) that will display the nth character from the existing file “MIRA.TXT”. If the total characters present are less thann, then it should display “invalid n”.
 
20. Write a function CountHisHer() in PYTHON which reads the contents of a text file “Gender.txt” which counts the words His and Her (not case sensitive) present in the file.
For, example, if the file contains:
Pankaj has gone to his friend’s house. His friend’s name is Ravya. Her house is 12KM from here.
The function should display the output:
Count of His: 2
Count of Her: 1
 
21 Write the function definition for WORD4CHAR() in PYTHON to read the content of a text file FUN.TXT, and display all those words, which have four characters in it.
Example:
If the content of the file Fun.TXT is as follows:
When I was a small child, I used to play in the garden with my grand mom. Those days were amazingly fun filled and I remember all the moments of that time
The function WORD4CHAR() should display the following:
When used play with days were that time

 

Please click on below link to download CBSE Class 12 Computer Science File Handling Worksheet Set A

File Handling CBSE Class 12 Computer Science Worksheet

Students can use the File Handling practice sheet provided above to prepare for their upcoming school tests. This solved questions and answers follow the latest CBSE syllabus for Class 12 Computer Science. You can easily download the PDF format and solve these questions every day to improve your marks. Our expert teachers have made these from the most important topics that are always asked in your exams to help you get more marks in exams.

NCERT Based Questions and Solutions for File Handling

Our expert team has used the official NCERT book for Class 12 Computer Science to create this practice material for students. After solving the questions our teachers have also suggested to study the NCERT solutions  which will help you to understand the best way to solve problems in Computer Science. You can get all this study material for free on studiestoday.com.

Extra Practice for Computer Science

To get the best results in Class 12, students should try the Computer Science MCQ Test for this chapter. We have also provided printable assignments for Class 12 Computer Science on our website. Regular practice will help you feel more confident and get higher marks in CBSE examinations.

Where can I download latest CBSE Practice worksheets for Class 12 Computer Science File Handling

You can download the CBSE Practice worksheets for Class 12 Computer Science File Handling for the latest session from StudiesToday.com

Are the Class 12 Computer Science File Handling Practice worksheets available for the latest session

Yes, the Practice worksheets issued for File Handling Class 12 Computer Science have been made available here for the latest academic session

Is there any charge for the Practice worksheets for Class 12 Computer Science File Handling

There is no charge for the Practice worksheets for Class 12 CBSE Computer Science File Handling you can download everything free

How can I improve my scores by solving questions given in Practice worksheets in File Handling Class 12 Computer Science

Regular revision of practice worksheets given on studiestoday for Class 12 subject Computer Science File Handling can help you to score better marks in exams

Are there any websites that offer free Practice test papers for Class 12 Computer Science File Handling

Yes, studiestoday.com provides all the latest Class 12 Computer Science File Handling test practice sheets with answers based on the latest books for the current academic session