How do I write a file in python?

Python
with open("time.log", "a") as f:
    f.write(f"your appended text\n")
    
with open("time.log", "w") as f:
    f.write(f"your overwriting text")

What is a file?

A file is a thing that stores data
The filename is the name of that thing

Files are made of bytes:

  • Some bytes are used to store readable text
    • Decoded with UTF-8 or ASCII
  • Some bytes are meant for programs only
    • PDFs and Excel files need special programs to be read

Plaintext files are meant to be read by humans as text

  • No special encoding or proprietary programs
  • People love plaintext because what you see is exactly what’s in the file.

How do you create files?

In Unix, Mac

  • “touch” makes an empty file
  • “>” output can be redirected into a file

touch notes.txt

  • Creates an empty file (if it doesn’t exist)
  • Updates the timestamp (if it does exist)

echo "hello world" > hello.txt

  • “>” writes output into a file
  • Creates the file contents (if it doesn’t exist)
  • Overwrites the file contents (if it does exist)

>> # append (add to the end)
> # overwrite

What can files represent?

  • Content
  • Logs
    • Typically write only, chronological, append-only
  • Databases
    • Your data is stored in files!
    • Databases are opinions of structure, querying, consistency, scale
  • “Everything is a file” (Unix philosophy)
    • Files, devices, network sockets, system info is all files:

Tutorial

Lets create a file using bash, and python

  • Time logger

Lets create some more files with python

  • Computer memory, cpu use logger
    • Write only
  • Position of the stars logger
    • Read and write
  • API call; weather API; average temperature; this time yesterday
    • Read and write and calculate

Part 1 — Creating files with bash

Bash
# touch 
touch notes.txt
ls

cat notes.txt

# >
echo "hello world" > hello.txt
cat hello.txt

echo "goodbye world" > hello.txt
cat hello.txt

# >>
echo "first line" > log.txt
echo "second line" >> log.txt
cat log.txt

echo "third line" >> log.txt
cat log.txt

Part 2 — Writing files with Python

Python
# bash
touch time_logger.py

from datetime import datetime, timezone

timestamp = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')

with open("time.log", "a") as f:
    f.write(f"{timestamp} time logged from python\n")

# bash
# run a couple times
python3 time_logger.py
python3 time_logger.py
python3 time_logger.py

cat time.log

Logging best practices

Use ISO 8601 format with UTC for timestamps:

  • 2026-01-27T09:00:10Z — sortable, unambiguous, parseable
  • The Z suffix means “Zulu time” (UTC)
  • Avoid local time in logs to prevent timezone confusion

Bash and Python can both write to the same file, but they must use the same format:

Bash
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) time logged from bash" >> time.log
python3 time_logger.py
cat time.log

Output:

2026-01-27T09:00:10Z time logged from bash
2026-01-27T09:00:15Z time logged from python

Part 3 — Reading and writing files with Python

A program can read from one file and write to another. This script reads all timestamps from time.log and calculates the average time between logs.

touch log_analyzer.py
Python
from datetime import datetime, timezone

# Read all timestamps from time.log
with open("time.log", "r") as f:
    lines = f.readlines()

# Parse timestamps
timestamps = []
for line in lines:
    timestamp_str = line.strip().split()[0]
    timestamp = datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone.utc)
    timestamps.append(timestamp)

# Calculate average time between logs
if len(timestamps) >= 2:
    differences = []
    for i in range(1, len(timestamps)):
        diff = (timestamps[i] - timestamps[i-1]).total_seconds()
        differences.append(diff)

    average = sum(differences) / len(differences)
    timestamp = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')

    log_line = f"{timestamp} average time between logs: {average:.1f} seconds"
    print(f"Writing to analysis.log: {log_line}")

    with open("analysis.log", "a") as f:
        f.write(f"{log_line}\n")

python3 log_analyzer.py

Output:

Writing to analysis.log: 2026-01-27T09:15:30Z average time between logs: 12.3 seconds

This demonstrates:

  • Reading all lines from a file ("r" mode)
  • Parsing structured data
  • Calculating differences between consecutive entries
  • Writing results to a different file