Python - File Handling FundamentalsWhich of the following is the correct syntax to open a file named 'data.txt' for reading in Python?Afile = open('data.txt', 'a')Bfile = open('data.txt', 'r')Cfile = open('data.txt', 'w')Dfile = open('data.txt', 'x')Check Answer
Step-by-Step SolutionSolution:Step 1: Understand file modes'r' is for reading, 'w' for writing, 'a' for appending, 'x' for exclusive creation.Step 2: Choose mode for readingTo read a file, use mode 'r'.Final Answer:file = open('data.txt', 'r') -> Option BQuick Check:Open for reading = mode 'r' [OK]Quick Trick: Use 'r' mode to open files for reading [OK]Common Mistakes:MISTAKESUsing 'w' or 'a' to read filesForgetting quotes around filenameUsing 'x' which creates new file only
Master "File Handling Fundamentals" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Custom Exceptions - Exception hierarchy - Quiz 3easy File Handling Fundamentals - Why file handling is required - Quiz 3easy File Reading and Writing Strategies - Reading entire file content - Quiz 12easy Magic Methods and Operator Overloading - Iterator protocol - Quiz 1easy Methods and Behavior Definition - Method invocation flow - Quiz 8hard Modules and Code Organization - __init__ file role - Quiz 14medium Modules and Code Organization - Importing specific items - Quiz 1easy Object-Oriented Programming Foundations - Procedural vs object-oriented approach - Quiz 12easy Polymorphism and Dynamic Behavior - Duck typing concept - Quiz 3easy Structured Data Files - Formatting structured data - Quiz 7medium