Python - Standard Library UsageWhich of the following is the correct syntax to open a file named 'data.txt' for writing in Python?Aopen('data.txt', 'r')Bopen('data.txt', 'w')Copen('data.txt', 'rw')Dopen('data.txt', 'a+')Check Answer
Step-by-Step SolutionSolution:Step 1: Identify the mode for writingThe mode 'w' opens a file for writing and creates it if it doesn't exist or overwrites if it does.Step 2: Check syntax correctnessopen('data.txt', 'w') is the correct syntax. 'r' is for reading, 'rw' is invalid, 'a+' is for appending and reading.Final Answer:open('data.txt', 'w') -> Option BQuick Check:Write mode = 'w' [OK]Quick Trick: Use 'w' to write or overwrite files [OK]Common Mistakes:Using 'r' when intending to writeUsing invalid mode 'rw'Confusing 'a+' with 'w'
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