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).

Frequently asked questions

How do you save file?
This task is covered in 2 stacks on this page: HTML & CSS, Python. The "Download Links" snippet in HTML & CSS uses `<a href="file-url" download="filename">Download</a>`.
Which code does the HTML & CSS example use?
The "Download Links" snippet uses `<a href="file-url" download="filename">Download</a>`, from the Links & Navigation section of the HTML & CSS cheat sheet.
Which stacks cover "save file" on this page?
HTML & CSS, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Download Links": The download attribute only works for same-origin URLs or blob/data URIs. Cross-origin files will open in the browser instead of downloading.