Chapter · File I/O Append a Log 追加日志 ≈ 20 min · python-file-io · Open in WolfCode Mode 'a' = append. Mode 'w' = erase. Pick the wrong one and your log loses history every call. Python · runnable Reset Run def append_log(path, message): with open(path, "a") as f: f.write(message + "\n") def read_log(path): with open(path) as f: return [line.rstrip("\n") for line in f] # Three calls — all three messages survive. append_log("audit.log", "user logged in") append_log("audit.log", "user clicked submit") append_log("audit.log", "user logged out") for line in read_log("audit.log"): print(line) Loading Python runtime (~5 MB, one-time)… From ATBS ch.9 ← Previous Read CSV-Lite Next → Count Lines