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

Frequently asked questions

How do you create file?
This task is covered in 2 stacks on this page: Bash & Linux, Python. The "Create Empty File / Update Timestamp" snippet in Bash & Linux uses `touch [options] file...`.
Which command does the Bash & Linux example use?
The "Create Empty File / Update Timestamp" snippet uses `touch [options] file...`, from the Navigation & Files section of the Bash & Linux cheat sheet.
Which stacks cover "create file" on this page?
Bash & Linux, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Create Empty File / Update Timestamp": 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.