File Handling in Python Class 12 NCERT Solutions

Teachers and Examiners (CBSESkillEduction) collaborated to create the File Handling in Python Class 12 NCERT Solutions. All the important Information are taken from the NCERT Textbook Computer Science (083) class 12.

File Handling in Python Class 12 NCERT Solutions

1. Differentiate between:
a) text file and binary file
Answer – Data files primarily come in two flavours: text files and binary files. Any text editor can open a text file since it is made up of characters that are human readable. Binary files, on the other hand, contain non-human readable characters and symbols and need special software to retrieve its information.

b) readline() and readlines()
Answer – When called, the readline() function in Python returns a line from the file. The readlines() method will return every line in a file as a list with each item representing a line in the file.

c) write() and writelines()
Answer – A string is passed as an argument to the write() method, which writes it to a text file. To write several strings to a file, use the writelines() method. To use the writelines() method, we must supply an iterable object (such as a list, tuple, etc.) that contains strings.

2. Write the use and syntax for the following methods:
a) open()
Answer – open(file_name, mode)
b) read()
Answer – file_object.read()
c) seek()
Answer – f.seek(offset, from_what)
d) dump()
Answer – dump(data_object, file_object)

3. Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files:
a) a text file “example.txt” in both read and write mode
b) a binary file “bfile.dat” in write mode
c) a text file “try.txt” in append and read mode
d) a binary file “btry.dat” in read only mode.
Answer –
a) file = open( “example.txt”, “w+” )
b)file = open(“bfile.dat” , “wb” )
c) file = open ( “try.txt” , “a+” )
d) file = open( “btry.dat” , “rb” )

4. Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some error message be flashed?
Answer – As Python ensures that any unwritten or unsaved data is flushed off (written to the file) before the file is closed, the main reason to close a file after read and write operations is to prevent the application from slowing down. So, once we’re done with it, it’s always a good idea to close the file.

5. What is the difference between the following set of statements (a) and (b):
a) P = open(“practice.txt”,”r”)
P.read(10)
b) with open(“practice.txt”, “r”) as P:
x = P.read()
Answer – File P will read 10 characters in part “a” but won’t print anything; in part “b,” however, it will read every character in the practise file and save it in the variable “x.”

6. Write a command(s) to write the following lines to the text file named hello.txt. Assume that the file is opened in append mode.
“ Welcome my class”
“It is a fun place”
“You will learn and play”
Answer –
f = open(“hello.txt”,”a”)
f.write(“Welcome my class”)
f.write(“It is a fun place”)
f.write(“You will learn and play”)
f.close()

7. Write a Python program to open the file hello.txt used in question no 6 in read mode to display its contents. What will be the difference if the file was opened in write mode instead of append mode?
Answer –
f = open(“hello.txt”,”r”)
x = f.read()
print(x)

8. Write a program to accept string/sentences from the user till the user enters “END” to. Save the data in a text file and then display only those sentences which begin with an uppercase alphabet.
Answer –
f = open(“example.txt”,”w”)
while True :
sen = input(“Enter something ( Enter END for quit ) :-“)
if sen == “END” :
break
else :
f.write(sen + “\n”)
f.close()
print()
print(“Lines started with Capital letters :-“)
f = open(“example.txt”,”r”)
print()
data = f.readlines()
for i in data :
if i[0].isupper() :
print(i)
f.close()

9. Define pickling in Python. Explain serialization and deserialization of Python object.
Answer – When serialising and deserializing a Python object structure, pickle is primarily employed. It involves transforming a Python object into a byte stream in order to store it in a file or database, keep programme state consistent across sessions, or send data over the network.

10. Write a program to enter the following records in a binary file:
Item No integer
Item_Name string
Qty integer
Price float
Number of records to be entered should be accepted from the user. Read the file to display the records in the following format:
Item No:
Item Name :
Quantity:
Price per item:
Amount: ( to be calculated as Price * Qty
Answer –
import pickle

file = open(“Pathwalla.dat”,”wb”)

while True :
dic = { }
Item_No = int(input(“Enter Item no.”))
Item_Name = input(“Enter Item name :-“)
Qty = int(input(“Enter Quantity :- “))
Price = float(input(“Enter price :-“))

dic[ “Item_No” ] = Item_No
dic[ “Item_Name” ] = Item_Name
dic[ “Qty” ] = Qty
dic[ “Price” ] = Price
pickle.dump( dic , file )
print()
ans = input(“Do you want to quit Enter [ Y/N ] :-“)
if ans == “y” or ans == “Y” :
break
print()

file.close()
print()
file = open(“Pathwalla.dat”,”rb”)

try :
while True :
dic = pickle.load(file)
print( “Item No. :”,”\t”, dic[ “Item_No” ] )
print( “Item Name :”,”\t”, dic[ “Item_Name” ] )
print( “Quantity :”,”\t”, dic[ “Qty” ] )
print( “Price of one :”,”\t”,dic[ “Price” ] )
print( “Amount :”, “\t” , dic[ “Qty” ] * dic[ “Price” ] )
print()
except :
file.close()

Computer Science Class 12 Notes

  1. Python Revision tour – 1 Class 12 Notes
  2. Python Revision tour – 2 Class 12 Notes Notes
  3. Working with Functions Class 12 Notes
  4. Using Python Libraries Class 12 Notes
  5. File Handling Class 12 Notes
  6. Recursion Class 12 Notes
  7. Idea of Algorithmic Efficiency Class 12 Notes
  8. Data Visualization using Pyplot Class 12 Notes
  9. Data Structure Class 12 Notes
  10. Computer Network Class 12 Notes
  11. More on MySQL Class 12 Notes

MCQ for Class 12 Computer Science Python

  1. Python Revision tour – 1 Class 12 MCQs
  2. Python Revision tour – 2 Class 12 MCQs
  3. Working with Functions Class 12 MCQs
  4. Using Python Libraries Class 12 MCQs
  5. File Handling Class 12 MCQs
  6. Recursion Class 12 MCQs
  7. Data Visualization using Pyplot Class 12 MCQs
  8. Data Structure Class 12 MCQs
  9. Computer Network Class 12 MCQs
  10. More on MySQL Class 12 MCQs

Computer Science Class 12 Questions and Answers

  1. Python Revision tour – 1 Class 12 Questions and Answers
  2. Working with Functions Class 12 Questions and Answers
  3. Using Python Libraries Class 12 Questions and Answers
  4. File Handling Class 12 Questions and Answers
  5. Recursion Class 12 Questions and Answers
  6. Idea of Algorithmic Efficiency Class 12 Questions and Answers
  7. Data Structure Class 12 Questions and Answers
  8. More on MySQL Class 12 Questions and Answers
  9. Computer Network Class 12 Questions and Answers
  10. More on MySQL Class 12 Questions and Answers 

Leave a Comment

error: Content is protected !!