Code Examples
# ── WRITING a file ───────────────────────────
with open("notes.txt", "w", encoding="utf-8") as f:
f.write("Line 1: Python is awesome!\n")
f.write("Line 2: File handling is easy.\n")
f.write("Line 3: Always use 'with'.\n")
# File is automatical...
import csv
# ── WRITING a CSV ─────────────────────────────
students = [
["Name", "Age", "GPA", "City"], # header row
["Ali", 20, 3.85, "Lahore"],
["Sara", 22, 3.92, "Karachi"],
["Fatima", 21, 3.75, "Islamabad"],
]
...
import json
data = {
"app" : "BitWithBite",
"version": "2.0",
"students": [
{"name": "Ali", "gpa": 3.85},
{"name": "Sara", "gpa": 3.92},
],
"active": True
}
# ── Write to JSON file ────────────────────────
with open...