Create file

2 snippets across 2 stacks — Bash & Linux, Python

SHBash & Linux

Create Empty File / Update Timestamp

SH · Navigation & Files
syntax
touch [options] file...
example
touch index.html
touch -t 202601151030 report.pdf

Note If the file exists, touch updates its modification timestamp without changing content. With -t, you can set a specific timestamp in [[CC]YY]MMDDhhmm[.ss] format.

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