Q9Multiple correct3 Marks1 Sep 2024
Select error-free implementations of a program that reads and prints `input.txt` line by line. If the file doesn't exist, the program should terminate without raising any error.
Option (A)
try:
f = open('input.txt', 'r')
```
try:
f = open('input.txt', 'r')
for line in f:
print(line.strip())
except FileNotFoundError:
print('File does not exist')
```
Option (B)
try:
f = open('input.txt', 'r')
```
try:
f = open('input.txt', 'r')
for line in f:
print(line.strip())
except:
print('File does not exist')
```
Option (C)
try:
f = open('input.txt', 'r')
```
try:
f = open('input.txt', 'r')
for line in f:
print(line.strip())
except InvalidError:
print('File does not exist')
```
Option (D)
try:
f = open('input.txt', 'r')
```
try:
f = open('input.txt', 'r')
for line in f:
print(line.strip())
except FileNotFoundError:
print('File does not exist')
except:
print('This is for all other errors that might come up')
```