Class 12 Computer Science
File Handling in Python
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: Difference between code (a) and code (b) are:
(a) In code (a), practice.txt will open in read mode and will read 10 bytes from practice.txt file. It does not close the file, because not close() function is called.
(b) In code (b), practice.txt will open in read mode and will read full file content from it. with statement will automatically close the file, when control move out of the with block.
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: Assumed this statement, as stated in questions –
filehandle = open(“hello.txt”, “a”)
Commands to write following lines are :-

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:
file_handle = open("hello.txt", "r") line = " " while line : line = file_handle.readline() print(line) file_handle.colse()
If the file was opened in write mode in question 6, then the above code would show these lines only –
Welcome my class
It is a fun place
You will learn and play
Because, write mode overwrite the contents of file, it open the file as new file i.e. blank file. While append does not overwrite the old contents, it keeps old one and add new at the end of the file.
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: Writing string/sentences in a text file “new.txt”
file_handle = open("new.txt", "w") line = " " while line != 'END': line = input("Enter a Sentence (END to exit": ") file_handle.write(line + "\n") file_handle.colse()
Reading string/sentences from a text file “new.txt”, which starts with capital alphabet.
file_handle = open("new.txt", "r") line = " " while line: line = file_handle.readline() if line: if line[0].isupper(): print(line) file_handle.colse()
9. Define pickling in Python. Explain serialization and deserialization of Python object.
Answer: Pickling or Serialization is the process of converting Python object hierarchy into a byte stream so that it can be written into a file.
Pickling converts an object byte stream in such a way that it can be reconstructed in original from when unpickled or deserialised.
Deserialization or Unpickling is the inverse process of Pickling where a byte stream is converted into an object hierarchy. Unpickling produces the exact replica of the original object.
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: Writing records in a Binary File:-
import pickle bin_file = open("Items.dat", "wb") record = {} wish = "Y" count = 1 while wish.upper() == "Y": print("Enter Following details of Item #",count) item_no = int(input("Enter Item Number : ")) item_name = input("Enter Item Name : ") quantity = int(input("Enter Quantity : ")) price = float(input("Enter Price per Item : ")) record['itemno"] = item_no record['itemname"] = item_name record['quantity"] = quantity record['price"] = price pickle.dump(record, bin_file) wish = input("Want to Add More Records (Y/N) : ") count = count + 1 bin_file.colse()
Reading records from a Binary File:-
import pickle bin_file = open("Items.dat", "rb") record = {} count = 1 try: while True: print("Item #",count,"Details") record = pickle.load(bin_file) print("Item Number : ",record['itemno']) print("Item Name : ",record['itemname']) print("Quantity : ",record['quantity']) print("Price : ", record['price']) print("Amount : ",record['quantity'] * record['price']) count = count + 1 except EOFError: bin_file.colse()
- Interface Python with MySQL – Important Questions – Answer
- Class 12 Computer Science – Exception Handling in Python NCERT Exercise Solutions
- Class 12 Computer Science File Handling in Python NCERT Exercise solution
- Class 12 Computer Science Stacks NCERT Exercise Solution
- Class 12 Computer Science Queues NCERT Exercise Solution
- Class 12 Computer Science Sorting NCERT Exercise Solution
- Class 12 Computer Science Chapter 8 Database Concepts NCERT Exercise Solution
- Class 12 Computer Science Structured Query Language SQL NCERT Book Exercise Solution
- Class 12 Computer Science Ch 10 Computer Network NCERT Book Exercise Solution
- Class 12 Computer Science Ch 11 Data Communication NCERT Book Exercise Solution