File I/O: Text, Binary, CSV, JSON

ফাইল I/O — text, binary, CSV, JSON

Read: ~28 min Intermediate 5 practice problems Live code runner

1. Persistence — Talking to the Disk

Variables live only while your program runs. To save work across runs — settings, scores, logs, results — you need persistent storage. The humblest form is a file. Python offers a single uniform open() function and thousands of libraries for every format on earth. This module covers the four you will actually use most often: plain text, binary, CSV, and JSON.

Variable শুধু প্রোগ্রাম চলার সময়ই বেঁচে থাকে। Run-এর পরও কিছু সংরক্ষণ করতে হলে — সেটিংস, score, log, ফলাফল — দরকার persistent storage। এর সবচেয়ে সহজ রূপ হলো ফাইল। Python-এ একটি uniform open() ফাংশন এবং পৃথিবীর প্রতিটি ফরম্যাটের জন্য হাজারো library আছে। এই module-এ চারটি সবচেয়ে ব্যবহৃত format: plain text, binary, CSV, JSON।

2. open() and the with Statement

Always use with when opening files. It guarantees the file is closed even if an exception occurs — no more leaked file handles. Inside the with block, the file object is yours to use.

basics.py
# Write text
with open("/tmp/hello.txt", "w", encoding="utf-8") as f:
    f.write("হ্যালো বাংলাদেশ!\n")
    f.write("Hello Bangladesh!\n")

# Read entire file
with open("/tmp/hello.txt", encoding="utf-8") as f:
    print(f.read())

# Read line by line — streaming, works for huge files
with open("/tmp/hello.txt", encoding="utf-8") as f:
    for line in f:
        print("→", line.rstrip())

# Append mode
with open("/tmp/hello.txt", "a", encoding="utf-8") as f:
    f.write("extra line\n")

3. File Modes

ModeMeaning
"r"Read text (default)
"w"Write, truncates existing file
"a"Append to end
"x"Exclusive create (fails if exists)
"b"Binary modifier (rb, wb)
"+"Read & write modifier (r+, w+)
Always pass encoding="utf-8" when opening text files. The default is OS-dependent and causes portability bugs with Bangla, emoji, and other non-ASCII text.

4. CSV Files with csv Module

CSV (comma-separated values) is the lingua franca of spreadsheets and datasets.

csv_demo.py
import csv

# Write
rows = [["name", "city", "age"],
        ["Asif", "Dhaka", 22],
        ["Mou", "Khulna", 21]]

with open("/tmp/people.csv", "w", newline="", encoding="utf-8") as f:
    csv.writer(f).writerows(rows)

# Read as dict (header row becomes keys)
with open("/tmp/people.csv", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(f"{row['name']} lives in {row['city']}")

5. JSON with the json Module

JSON is the standard format for web APIs and configuration. Python's json module converts between Python objects and JSON text.

json_demo.py
import json

profile = {
    "name": "Ayesha",
    "city": "Chittagong",
    "languages": ["Bangla", "English", "Python"],
    "active": True
}

# Python → JSON string
text = json.dumps(profile, ensure_ascii=False, indent=2)
print(text)

# Python → JSON file
with open("/tmp/profile.json", "w", encoding="utf-8") as f:
    json.dump(profile, f, ensure_ascii=False, indent=2)

# JSON file → Python
with open("/tmp/profile.json", encoding="utf-8") as f:
    loaded = json.load(f)
print(loaded["languages"])

6. Binary Files

Use binary mode for images, audio, or any format where "character" is not meaningful.

binary.py
# Write raw bytes
with open("/tmp/blob.bin", "wb") as f:
    f.write(bytes([0x50, 0x4B, 0x03, 0x04]))

# Read raw bytes
with open("/tmp/blob.bin", "rb") as f:
    data = f.read()

print(data)
print("Is ZIP?", data.startswith(b"PK\x03\x04"))

7. pathlib — Modern Path Handling

pathlib_demo.py
from pathlib import Path

p = Path("/tmp/notes")
p.mkdir(exist_ok=True)

f = p / "hello.txt"
f.write_text("Pathlib makes paths pleasant.", encoding="utf-8")

print(f.exists(), f.read_text())
print(list(p.iterdir()))

8. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
File handleOS resource representing an open file.OS-এর দেওয়া একটি open ফাইল resource।
EncodingHow text bytes map to characters (UTF-8, etc.).Text-এর byte-কে character-এ রূপান্তরের নিয়ম।
CSVComma-separated values — plain text table.কমা-বিভক্ত মান — সাধারণ text টেবিল।
JSONJavaScript Object Notation — hierarchical text.JavaScript Object Notation — stepped text format।
Context managerPattern behind with — auto cleanup.with-এর পেছনের pattern — auto cleanup।

9. Practice Problems

  1. Write a program that writes "Hello" to /tmp/a.txt then reads it back.
    /tmp/a.txt-এ "Hello" লিখুন এবং পরে পড়ে দেখান।
    ✨ Show Answer (উত্তর দেখুন)
    ans1.py
    with open("/tmp/a.txt", "w") as f:
        f.write("Hello")
    print(open("/tmp/a.txt").read())
  2. Count the number of lines in a file. (Use the one you just wrote.)
    একটি ফাইলে কয়টি লাইন আছে তা গুনুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans2.py
    with open("/tmp/multi.txt", "w") as f:
        f.write("a\nb\nc\nd\n")
    
    with open("/tmp/multi.txt") as f:
        print(sum(1 for _ in f))
  3. Write 3 rows to a CSV, then read them using csv.DictReader.
    CSV-এ ৩টি row লিখুন, পরে DictReader দিয়ে পড়ুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans3.py
    import csv
    with open("/tmp/s.csv", "w", newline="") as f:
        w = csv.writer(f)
        w.writerow(["id", "name"])
        w.writerows([[1, "A"], [2, "B"], [3, "C"]])
    
    with open("/tmp/s.csv") as f:
        for row in csv.DictReader(f):
            print(row)
  4. Save a Python dict with a Bangla value as JSON (preserving Bangla characters).
    Bangla value-সহ একটি dict-কে JSON-এ সংরক্ষণ করুন — Bangla অক্ষর যেন অটুট থাকে।
    ✨ Show Answer (উত্তর দেখুন)
    ans4.py
    import json
    data = {"name": "রাফি", "city": "ঢাকা"}
    with open("/tmp/bn.json", "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False)
    
    print(open("/tmp/bn.json", encoding="utf-8").read())
  5. Explain why with open(...) is safer than calling .close() manually.
    ব্যাখ্যা করুন: manually .close() ডাকার চেয়ে with open(...) কেন নিরাপদ।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: If an exception is raised between open() and your .close() call, the file is never closed — leaking the file handle and potentially losing buffered writes. The with statement uses a context manager that guarantees cleanup even on exceptions, so the file is always closed. This is both safer and less code.

    open() ও .close()-এর মাঝে exception হলে file বন্ধ হয় না — handle leak ও buffered write হারানোর ঝুঁকি থাকে। with একটি context manager ব্যবহার করে যা exception হলেও cleanup নিশ্চিত করে — নিরাপদ ও ছোট কোড।

Summary — Module 20

open() with with is your universal file-handling pattern. Always specify encoding="utf-8" for text. Use csv for tables and json for structured data. pathlib makes path manipulation elegant. Binary mode (rb, wb) is for non-text formats.

open() ও with — সব ধরনের file handling-এর ভিত্তি। Text-এ সবসময় encoding="utf-8" দিন। Table-এ csv, structured data-য় json, path manipulate-এ pathlib, non-text ফাইলে binary mode।

Next Module → Modules, Packages, pip & venv।