How do you open a file in Python?
-
A
open_file("file.txt")
-
B
file.open("file.txt")
-
C
with open("file.txt", 'r') as f:
Correct answer
-
D
read_file("file.txt")
Correct answer
C. with open("file.txt", 'r') as f:
15 views
Explanation
The correct way to open a file in Python is demonstrated in option 'c', using the 'with open' syntax, which ensures proper handling of resources.
Doubts & discussion
Not sure why the answer is C? Ask here and get an explanation.
Ask a doubt about this MCQ