Question 5 of 10intermediate🔍 AnalyzeShort Answer2 marks

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

(a)
(b)

Explanation

This question tests understanding of file handling in Python. The key differences are: (1) The 'with' statement is a context manager that automatically handles file closing, while the basic open() requires explicit close() - this is important because unclosed files can cause resource leaks. (2) The read(n) method reads specified number of characters, while read() without arguments reads the entire file. The textbook emphasizes the importance of closing files in Question 4, and shows examples of both explicit closing (Program 2-4) and the with statement usage.