1. Home
  2. Data Science
  3. Python
  4. How do you open a file in Python?

Added 5 Feb 2024

How do you open a file in Python?

  1. open_file("file.txt")
  2. file.open("file.txt")
  3. with open("file.txt", 'r') as f: Correct answer
  4. read_file("file.txt")

Correct answer

C. with open("file.txt", 'r') as f:

15 views

MCQSEXAM

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.

Reference

https://www.w3schools.com/python/default.asp

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs