# 7 mistakes almost every beginner makes with file handling.
# Wrong way, error, right way — all in one file.
# redhorndev.com

import csv

# ─────────────────────────────────────────────
# 1. Not closing the file
# ─────────────────────────────────────────────

# WRONG — manual open without close
# f = open("soldiers.txt", "r")
# content = f.read()
# f.close() — easy to forget

# CORRECT — with open() closes automatically
with open("soldiers.txt", "r", encoding="utf-8") as f:
    content = f.read()

# ─────────────────────────────────────────────
# 2. "w" mode — silent overwrite
# ─────────────────────────────────────────────

# WRONG — existing content gone, no warning
# with open("roster.txt", "w", encoding="utf-8") as f:
#     f.write("Raven\n")

# CORRECT — use "a" to keep existing data
with open("roster.txt", "a", encoding="utf-8") as f:
    f.write("Raven\n")

# use "w" only when you intend to replace the file completely

# ─────────────────────────────────────────────
# 3. FileNotFoundError — wrong path
# ─────────────────────────────────────────────

# WRONG — no protection
# with open("missing.txt", "r") as f:
#     content = f.read()         # FileNotFoundError

# CORRECT — handle it
try:
    with open("soldiers.txt", "r", encoding="utf-8") as f:
        content = f.read()
except FileNotFoundError:
    print("File not found. Check the path.")

# ─────────────────────────────────────────────
# 4. Missing \n — everything on one line
# ─────────────────────────────────────────────

soldiers = ["Raven", "Wolf", "Ghost"]

# WRONG — produces "RavenWolfGhost"
with open("roster.txt", "w", encoding="utf-8") as f:
    for soldier in soldiers:
        f.write(soldier)

# CORRECT — add \n manually
with open("roster.txt", "w", encoding="utf-8") as f:
    for soldier in soldiers:
        f.write(soldier + "\n")

# ─────────────────────────────────────────────
# 5. CSV values are strings — forgetting to convert
# ─────────────────────────────────────────────

# WRONG
# with open("soldiers.csv", "r", encoding="utf-8") as f:
#     reader = csv.DictReader(f)
#     for row in reader:
#         print(row["score"] + 10)    # TypeError — str + int

# CORRECT
with open("soldiers.csv", "r", encoding="utf-8") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(int(row["score"]) + 10)

# ─────────────────────────────────────────────
# 6. Missing newline="" when writing CSV
# ─────────────────────────────────────────────

rows = [["name", "score"], ["Raven", 85], ["Wolf", 74]]

# WRONG — blank lines between rows on Windows
# with open("output.csv", "w", encoding="utf-8") as f:
#     writer = csv.writer(f)
#     writer.writerows(rows)

# CORRECT
with open("output.csv", "w", encoding="utf-8", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(rows)

# ─────────────────────────────────────────────
# 7. Reading a file twice without reopening
# ─────────────────────────────────────────────

with open("soldiers.txt", "r", encoding="utf-8") as f:
    first  = f.read()
    second = f.read()   # WRONG — pointer at end, returns ""

print(len(first))       # full content
print(len(second))      # 0 — empty

# CORRECT — store content in a variable and reuse it
with open("soldiers.txt", "r", encoding="utf-8") as f:
    content = f.read()

print(content)
print(content)          # reuse the variable — no problem

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# always use with open()            — closes automatically
# "w" overwrites silently           — use "a" to preserve
# wrap reads in try/except          — FileNotFoundError
# add \n manually                   — write() doesn't add it
# CSV values are strings            — convert with int(), float()
# newline="" when writing CSV       — prevents blank lines
# read() moves pointer to end       — store in variable to reuse
