# Write, Overwrite, Append: How Python Handles File Output
# "w" creates or overwrites — "a" adds to the end
# redhorndev.com

# ─────────────────────────────────────────────
# write() — create or overwrite
# ─────────────────────────────────────────────

with open("roster.txt", "w", encoding="utf-8") as f:
    f.write("Raven\n")
    f.write("Wolf\n")
    f.write("Ghost\n")

# roster.txt now contains:
# Raven
# Wolf
# Ghost

# run again — same result, previous content gone

# ─────────────────────────────────────────────
# writelines() — write a list at once
# ─────────────────────────────────────────────

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

with open("roster.txt", "w", encoding="utf-8") as f:
    f.writelines(soldiers)      # \n must be in the strings

# same result as above

# ─────────────────────────────────────────────
# append() — add without destroying
# ─────────────────────────────────────────────

with open("roster.txt", "a", encoding="utf-8") as f:
    f.write("Viper\n")
    f.write("Bull\n")

# roster.txt now contains:
# Raven / Wolf / Ghost / Viper / Bull

# ─────────────────────────────────────────────
# Writing data from a list — standard pattern
# ─────────────────────────────────────────────

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

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

# ─────────────────────────────────────────────
# Writing numbers — convert to string first
# ─────────────────────────────────────────────

scores = {"Raven": 85, "Wolf": 74, "Ghost": 91}

with open("scores.txt", "w", encoding="utf-8") as f:
    for name, score in scores.items():
        f.write(f"{name},{score}\n")

# scores.txt:
# Raven,85
# Wolf,74
# Ghost,91

# ─────────────────────────────────────────────
# Verify — read back what was written
# ─────────────────────────────────────────────

with open("roster.txt", "r", encoding="utf-8") as f:
    print(f.read())

# ─────────────────────────────────────────────
# "w" vs "a" — side by side
# ─────────────────────────────────────────────

# "w" — run twice, still one line
with open("test_w.txt", "w", encoding="utf-8") as f:
    f.write("Line from run 1\n")

with open("test_w.txt", "w", encoding="utf-8") as f:
    f.write("Line from run 2\n")

# test_w.txt contains only: Line from run 2

# "a" — run twice, two lines
with open("test_a.txt", "a", encoding="utf-8") as f:
    f.write("Line from run 1\n")

with open("test_a.txt", "a", encoding="utf-8") as f:
    f.write("Line from run 2\n")

# test_a.txt contains:
# Line from run 1
# Line from run 2

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

# open("file.txt", "w")       — create or overwrite
# open("file.txt", "a")       — append to end
# f.write(string)             — write one string (add \n manually)
# f.writelines(list)          — write list of strings (add \n in strings)
#
# "w" overwrites silently — no warning, no undo
# write() requires strings — use str() or f-strings for numbers
# file created in same folder as script if no path specified
