To write data, use 'w' mode and the write() method inside a with block for safety.
Step 2: Check each option
file = open('greet.txt', 'r'); file.write('Hello'); file.close() uses 'r' mode which is read-only, so write() will fail. open('greet.txt', 'w').read('Hello') uses read() instead of write(). with open('greet.txt', 'a') as file: file.read('Hello') uses read() and 'a' mode but tries to read data, which is incorrect. with open('greet.txt', 'w') as file: file.write('Hello') correctly uses 'w' mode and write() inside a with block.
Final Answer:
with open('greet.txt', 'w') as file: file.write('Hello') -> Option A
Quick Check:
Use with + 'w' + write() to save text [OK]
Quick Trick:Use with open(filename, 'w') and write() to save text [OK]
Common Mistakes:
Using 'r' mode when writing
Calling read() instead of write()
Not closing the file or missing with block
Master "File Handling Fundamentals" in Python
9 interactive learning modes - each teaches the same concept differently