File Handling in Python

Class 12 · Computer Science · 10 Questions

1intermediate💡 Understand2 marksShort Answer

For a newly created file, is there any difference between write() and append() methods?

1beginner💡 Understand3 marksShort Answer

Differentiate between: a) text file and binary file b) readline() and readlines() c) write() and writelines()

3beginner🔧 Apply4 marksShort Answer

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.

4intermediate💡 Understand2 marksShort Answer

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?

5intermediate🔍 Analyze2 marksShort Answer

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()

6beginner🔧 Apply2 marksShort Answer

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”

7intermediate🔧 Apply3 marksLong Answer

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?

8intermediate🔧 Apply4 marksLong Answer

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.

9intermediate💡 Understand2 marksShort Answer

Define pickling in Python. Explain serialization and deserialization of Python object.

10advanced🔧 Apply5 marksLong Answer

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)