Q.Q. 17 and 18 are Assertion (A) and Reasoning (R) based questions. Mark the correct choice as
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The readlines() method reads all lines into a list, while readline() reads one line at a time, requiring a loop to process an entire file.
When working with text files in Python, a common task is to read their contents. Python provides several methods for this, each suited for different scenarios. Understanding the nuances of these methods, particularly readlines() and readline(), is crucial for efficient and correct file handling. These methods allow us to access the data stored in a file, whether we need it all at once or line by line.
The readlines() method is designed for situations where you need to load the entire content of a text file into memory as a collection of individual lines. When you call readlines() on an open file object, it performs the following actions:
- It reads every single line from the current position in the file until the end of the file is reached.
- It collects these lines into a list.
- Each element in this list is a string, representing one line from the file.
- Crucially, each of these string elements retains the newline character (
\n) at its end, which marks the termination of a line in the text file.
The newline character (\n) is an invisible character that signifies the end of a line and causes the cursor to move to the beginning of the next line. When readlines() returns a list of strings, these \n characters are included in the strings, unless they are explicitly removed (e.g., using strip()).
In contrast, the readline() method operates differently. Its purpose is to read a file one line at a time, which is particularly useful when dealing with very large files that might not fit entirely into memory, or when you need to process each line individually before moving to the next.
- When
readline()is called, it reads only the next single line from the file. - It returns this line as a single string.
- Similar to
readlines(), this string also includes the newline character (\n) at its end, if present in the file. - If
readline()is called again, it reads the subsequent line. This sequential reading continues until the end of the file. When the end of the file is reached, subsequent calls toreadline()will return an empty string ('').
To read the entire text file line by line using readline(), you must employ a looping construct, such as a while loop or a for loop (though a for loop directly over the file object is often more Pythonic for line-by-line iteration). Without a loop, readline() will only ever read the first line (or the next single line if called multiple times manually).
Now, let's evaluate the given Assertion and Reasoning: …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.