Python CLI Snippets#
Standalone scripts β save each to a .py file and run with python script.py.
File I/O β read, filter, write#
Read a text file, filter lines containing a keyword, and write results to a new file.
#!/usr/bin/env python3
# filter_lines.py
import sys
keyword = sys.argv[1] if len(sys.argv) > 1 else "error"
src = sys.argv[2] if len(sys.argv) > 2 else "input.txt"
dst = sys.argv[3] if len(sys.argv) > 3 else "output.txt"
with open(src) as f:
lines = f.readlines()
matched = [l for l in lines if keyword.lower() in l.lower()]
with open(dst, "w") as f:
f.writelines(matched)
print(f"Filtered {len(matched)}/{len(lines)} lines β {dst}")
# Create a sample input
printf "INFO: server started\nERROR: connection refused\nINFO: done\nERROR: timeout\n" > input.txt
python filter_lines.py error input.txt errors.txt
Output:
Filtered 2/4 lines β errors.txt
errors.txt contents:
ERROR: connection refused
ERROR: timeout
JSON β load, transform, dump#
Read a JSON file, add/transform a field, and write the result back out.
#!/usr/bin/env python3
# transform_json.py
import json
import sys
from pathlib import Path
src = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("data.json")
dst = Path(sys.argv[2]) if len(sys.argv) > 2 else Path("data_out.json")
records = json.loads(src.read_text())
# Add a computed field to every record
for rec in records:
rec["label"] = f"{rec['name']} (age {rec['age']})"
dst.write_text(json.dumps(records, indent=2))
print(f"Wrote {len(records)} records to {dst}")
# Create sample input
echo '[{"name":"Alice","age":30},{"name":"Bob","age":25}]' > data.json
python transform_json.py data.json data_out.json
cat data_out.json
Output:
Wrote 2 records to data_out.json
data_out.json contents:
[
{
"name": "Alice",
"age": 30,
"label": "Alice (age 30)"
},
{
"name": "Bob",
"age": 25,
"label": "Bob (age 25)"
}
]
HTTP request β stdlib (no dependencies)#
Fetch a URL and print the response body using only the standard library.
#!/usr/bin/env python3
# fetch.py
import json
import sys
import urllib.request
url = sys.argv[1] if len(sys.argv) > 1 else "https://httpbin.org/get"
with urllib.request.urlopen(url, timeout=10) as resp:
body = resp.read().decode()
status = resp.status
print(f"Status: {status}")
try:
parsed = json.loads(body)
print(json.dumps(parsed.get("headers", parsed), indent=2))
except json.JSONDecodeError:
print(body[:500])
python fetch.py https://httpbin.org/get
Output:
Status: 200
{
"Accept": "*/*",
"Accept-Encoding": "identity",
"Host": "httpbin.org",
"User-Agent": "Python-urllib/3.12"
}
[!TIP] For POST, auth, session reuse, or retries, use requests or httpx instead.
HTTP request β with requests#
#!/usr/bin/env python3
# post_json.py
import json, sys
import requests
url = "https://httpbin.org/post"
payload = {"action": "test", "value": 42}
resp = requests.post(url, json=payload, timeout=10)
resp.raise_for_status()
result = resp.json()
print(f"Status: {resp.status_code}")
print("Echo'd JSON:", json.dumps(result["json"], indent=2))
python post_json.py
Output:
Status: 200
Echo'd JSON: {
"action": "test",
"value": 42
}
CSV processing β compute summary stats#
Read a CSV file and print per-column min, max, and mean for numeric columns.
#!/usr/bin/env python3
# csv_stats.py
import csv, sys
from collections import defaultdict
path = sys.argv[1] if len(sys.argv) > 1 else "data.csv"
columns: dict[str, list[float]] = defaultdict(list)
with open(path, newline="") as f:
reader = csv.DictReader(f)
for row in reader:
for key, val in row.items():
try:
columns[key].append(float(val))
except (ValueError, TypeError):
pass # skip non-numeric columns
print(f"{'Column':<20} {'Min':>10} {'Max':>10} {'Mean':>10}")
print("-" * 54)
for col, values in columns.items():
print(f"{col:<20} {min(values):>10.2f} {max(values):>10.2f} {sum(values)/len(values):>10.2f}")
printf "name,age,salary\nAlice,30,90000\nBob,25,75000\nCarol,35,105000\n" > data.csv
python csv_stats.py data.csv
Output:
Column Min Max Mean
------------------------------------------------------
age 25.00 35.00 30.00
salary 75000.00 105000.00 90000.00
Data processing β word frequency counter#
Count word frequencies in a text file and print the top N.
#!/usr/bin/env python3
# word_freq.py
import re, sys
from collections import Counter
from pathlib import Path
path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("text.txt")
top_n = int(sys.argv[2]) if len(sys.argv) > 2 else 10
text = path.read_text(encoding="utf-8", errors="ignore").lower()
words = re.findall(r"\b[a-z]{3,}\b", text) # only words β₯ 3 chars
counter = Counter(words)
print(f"Top {top_n} words in {path.name} ({len(words)} total):\n")
for word, count in counter.most_common(top_n):
bar = "β" * (count * 20 // counter.most_common(1)[0][1])
print(f" {word:<15} {count:>5} {bar}")
echo "the quick brown fox jumps over the lazy dog the fox" > text.txt
python word_freq.py text.txt 5
Output:
Top 5 words in text.txt (10 total):
the 3 ββββββββββββββββββββ
fox 2 βββββββββββββ
quick 1 ββββββ
brown 1 ββββββ
jumps 1 ββββββ
Walk a directory tree#
Print all .py files under a directory with their sizes.
#!/usr/bin/env python3
# find_py.py
import sys
from pathlib import Path
root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".")
total = 0
for p in sorted(root.rglob("*.py")):
size = p.stat().st_size
total += size
print(f"{size:>8,} B {p}")
print(f"\nTotal: {total:,} bytes across {sum(1 for _ in root.rglob('*.py'))} files")
python find_py.py src/
Output:
4,821 B src/app.py
1,203 B src/utils.py
847 B src/models.py
Total: 6,871 bytes across 3 files