File I/O & JSON

Read, write, and parse files in Python — including text logs, CSV reports, and JSON configuration files used in security tools.

Medium 55m 3 tasks

Learning Objectives

  • Open, read, and write files safely using context managers
  • Parse text files line-by-line for log analysis
  • Read and write JSON data with the json module
  • Use the csv module to handle scan reports
  • Handle file I/O exceptions gracefully

Opening Files

Always use a context manager (with statement) — it closes the file automatically:

with open("targets.txt", "r") as f:
    content = f.read()   # entire file as string

# Read line by line (memory-efficient for large logs)
with open("access.log", "r") as f:
    for line in f:
        print(line.strip())

Modes: "r" (read), "w" (write, truncates), "a" (append), "rb" / "wb" (binary).

Reading Methods

with open("ips.txt") as f:
    lines = f.readlines()    # list of strings including \n
    # OR
    lines = f.read().splitlines()   # list without \n

Writing Files

hosts = ["192.168.1.1", "192.168.1.2", "10.0.0.1"]
with open("output.txt", "w") as f:
    for host in hosts:
        f.write(host + "\n")

# Append mode
with open("scan.log", "a") as f:
    f.write("Scan started at 2024-01-15 10:00\n")

Handling Exceptions

try:
    with open("config.txt") as f:
        data = f.read()
except FileNotFoundError:
    print("Config file not found — using defaults")
except PermissionError:
    print("Permission denied reading config")

JSON

JSON is the standard format for tool configs, API responses, and scan outputs:

import json

# Parsing JSON string
raw = '{"host": "10.0.0.1", "ports": [22, 80]}'
data = json.loads(raw)
print(data["host"])       # 10.0.0.1
print(data["ports"][0])   # 22

# Reading a JSON file
with open("scan.json") as f:
    scan = json.load(f)

# Writing JSON
result = {"host": "10.0.0.1", "open_ports": [22, 80, 443]}
with open("result.json", "w") as f:
    json.dump(result, f, indent=2)

# To a string
print(json.dumps(result, indent=2))

CSV

import csv

# Read
with open("targets.csv", newline="") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row["ip"], row["port"])

# Write
rows = [
    {"ip": "10.0.0.1", "port": "22", "status": "open"},
    {"ip": "10.0.0.2", "port": "80", "status": "filtered"},
]
with open("results.csv", "w", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=["ip", "port", "status"])
    writer.writeheader()
    writer.writerows(rows)

Log Parsing Example

import re

def parse_auth_log(path):
    failures = []
    pattern = re.compile(r"Failed password for (\S+) from (\S+)")
    with open(path) as f:
        for line in f:
            m = pattern.search(line)
            if m:
                failures.append({"user": m.group(1), "ip": m.group(2)})
    return failures

# failures = parse_auth_log("/var/log/auth.log")

Mini Project – Scan Logger

import json
from datetime import datetime

def log_scan(host, open_ports, path="scans.json"):
    try:
        with open(path) as f:
            data = json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        data = []

    data.append({
        "timestamp": datetime.now().isoformat(),
        "host":      host,
        "ports":     open_ports,
    })

    with open(path, "w") as f:
        json.dump(data, f, indent=2)
    print(f"Logged {host}: {open_ports}")

log_scan("192.168.1.1", [22, 80, 443])
log_scan("192.168.1.2", [3389])

Create a file ips.txt with 10 IP addresses (one per line, include some duplicates). Write a script that reads the file and prints only unique IPs using a set.

✦ Answer the questions to complete this task

What context manager keyword ensures a file is closed after use?

What mode string opens a file for reading in text mode?

Implement the Scan Logger mini project. Run it 3 times with different hosts/ports. Then read back scans.json and print the host with the most open ports.

✦ Answer the questions to complete this task

Which json function reads a JSON file object?

Which json function converts a Python object to a JSON string?

Write a script that creates a list of 5 scan result dicts (ip, port, status) and writes them to results.csv using csv.DictWriter. Then read it back and print only rows where status='open'.

✦ Answer the questions to complete this task

What argument to DictWriter specifies the column names?

💪 Exercises & Challenges

📝 MCQ Medium +20 XP

File I/O & JSON MCQ

MCQ: File I/O & JSON MCQ

Start →
⚙️ Practical Medium +30 XP

Auth Log Analyzer

Auth Log Analyzer

Start →
🚩 Challenge Hard +50 XP

Hidden Config Flag

Hidden Config Flag

Start →