Python - Standard Library UsageWhich of the following is the correct syntax to open a file named 'log.txt' for reading in Python?Aopen('log.txt', 'w')Bopen('log.txt', 'a')Copen('log.txt', 'r')Dopen('log.txt', 'x')Check Answer
Step-by-Step SolutionSolution:Step 1: Identify the mode for readingThe mode 'r' is used to open a file for reading only.Step 2: Check other modes'w' is for writing (overwrites), 'a' is for appending, and 'x' is for creating a new file and writing. So only 'r' fits reading.Final Answer:open('log.txt', 'r') -> Option CQuick Check:Read mode = 'r' [OK]Quick Trick: Use 'r' mode to read files safely [OK]Common Mistakes:Using 'w' instead of 'r'Confusing 'a' with read modeUsing 'x' which fails if file exists
Master "Standard Library Usage" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Class Methods and Static Methods - Class methods and cls usage - Quiz 7medium Custom Exceptions - Adding custom attributes - Quiz 7medium Custom Exceptions - Best practices for custom exceptions - Quiz 1easy Custom Exceptions - Extending built-in exceptions - Quiz 5medium Encapsulation and Data Protection - Public attributes - Quiz 1easy Exception Handling Fundamentals - Common exception types - Quiz 15hard File Reading and Writing Strategies - Overwrite vs append behavior - Quiz 4medium Methods and Behavior Definition - Methods with return values - Quiz 13medium Multiple Inheritance and Method Resolution - Method Resolution Order (MRO) - Quiz 8hard Structured Data Files - Reading and writing CSV data - Quiz 1easy