Class 12 Computer Science Practice Set: File Handling
Access printable practice worksheets for File Handling designed to align with the 2026-27 academic syllabus for Class 12 Computer Science. These structured exercises help students evaluate their conceptual understanding and improve exam readiness.
Download File Handling Worksheet PDF
View or download the dedicated File Handling practice resource below. Engaging with these objective and subjective questions daily ensures continuous academic progress and mastery of the 2026-27 curriculum.
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
Please click on below link to download CBSE Class 12 Computer Science File Handling Worksheet Set A
Free study material for Computer Science
Download CBSE Practice Material: Class 12 Computer Science File Handling
File Handling Printable Worksheet for Class 12 Computer Science
Explore reliable practice questions for File Handling tailored for Class 12 Computer Science learners. Use these structured worksheets to evaluate preparedness and strengthen problem-solving skills.
How to Use These Practice Sheets
Cross-reference your completed exercises with comprehensive NCERT solutions for Class 12 Computer Science to ensure absolute clarity across all sub-topics in this chapter.
Enhance Speed with Online Practice
Explore our broader library of printable assignments, chapter notes, and mock tests designed to support continuous revision and secure higher marks in CBSE assessments.
FAQs
You can download the teacher-verified PDF for CBSE Class 12 Computer Science File Handling Worksheet Set 01 from StudiesToday.com. These practice sheets for Class 12 Computer Science are designed as per the latest CBSE academic session.
Yes, our CBSE Class 12 Computer Science File Handling Worksheet Set 01 includes a variety of questions like Case-based studies, Assertion-Reasoning, and MCQs as per the 50% competency-based weightage in the latest curriculum for Class 12.
Yes, we have provided detailed solutions for CBSE Class 12 Computer Science File Handling Worksheet Set 01 to help Class 12 and follow the official CBSE marking scheme.
Daily practice with these Computer Science worksheets helps in identifying understanding gaps. It also improves question solving speed and ensures that Class 12 students get more marks in CBSE exams.
All our Class 12 Computer Science practice test papers and worksheets are available for free download in mobile-friendly PDF format. You can access CBSE Class 12 Computer Science File Handling Worksheet Set 01 without any registration.