Friday , September 20 2024

The following line of code: for line in open(‘text.txt’, ‘rt’):

Questions BankCategory: Python Essentials 2The following line of code: for line in open(‘text.txt’, ‘rt’):
The following line of code:

for line in open('text.txt', 'rt'):
  • is invalid because open returns a non-iterable object
  • is invalid because open returns nothing
  • is valid because open returns an iterable object
  • may be valid if line is a list

Explanation: The open() method returns an iterable object, which can be used to iterate through all the file’s lines inside a for loop.

More Questions: Python Essentials 2 (PE2) Course Final Test