Programming Languages
Python
Subjective
Sep 30, 2025
How do you read and write files in Python? Show both methods.
Detailed Explanation
# Method 1 - with statement (recommended):\nwith open("file.txt", "r") as f:\n content = f.read()\n\nwith open("file.txt", "w") as f:\n f.write("Hello World")\n\n# Method 2 - manual close:\nf = open("file.txt", "r")\ncontent = f.read()\nf.close()
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts