Save file

2 snippets across 2 stacks — HTML & CSS, Python

HCHTML & CSS

Download Links

HC · Links & Navigation
syntax
<a href="file-url" download="filename">Download</a>
example
<a href="/reports/q4-summary.pdf" download="Q4-Report.pdf">
  Download Q4 Report
</a>

Note The download attribute only works for same-origin URLs or blob/data URIs. Cross-origin files will open in the browser instead of downloading.

PYPython

Writing Files

PY · File I/O
syntax
with open(path, 'w') as f:
    f.write(text)
example
lines = ["alice,95", "bob,82", "carol,91"]

with open("scores.csv", "w", encoding="utf-8") as f:
    f.write("name,score\n")
    for line in lines:
        f.write(line + "\n")

# Append mode
with open("scores.csv", "a") as f:
    f.write("dave,88\n")

Note 'w' mode truncates the file first. Use 'a' to append. Use 'x' to create exclusively (fails if file exists).