Which of these correctly opens a file for reading and guarantees it is closed?
- A f = open('data.txt'); f.read()
- B with open('data.txt') as f: f.read()
- C file = read('data.txt')
- D open('data.txt').close()
Answer
with open('data.txt') as f: f.read()
The with statement closes the file even if an exception occurs inside the block, which manual open and close does not guarantee.





