Examples / Standard Library / File Io

File Io

Has Specs

Comprehensive demonstration of file I/O operations across different formats. This example shows: - Reading CSV files with automatic header handling - Processing data with Lua table operations - Writing results to multiple formats (CSV, JSON, text) - Using `require()` to import I/O libraries (`tactus.io.csv`, `tactus.io.json`, `tactus.io.file`) - Relative file paths from procedure directory - Working with the gitignored `output/` folder

Source Code

--[[
Example: File I/O Operations

Demonstrates reading and writing various file formats in Tactus.
All file operations are restricted to the current working directory.

Available libraries (via require):
- tactus.io.file: Raw text read/write
- tactus.io.json: JSON encode/decode
- tactus.io.csv: CSV read/write with automatic header handling
- tactus.io.tsv: Tab-separated values read/write
- tactus.io.parquet: Apache Parquet format (requires pyarrow)
- tactus.io.hdf5: HDF5 datasets (requires h5py)
- tactus.io.excel: Excel spreadsheets (requires openpyxl)

To run:
  tactus run examples/52-file-io-basics.tac
]]--

local csv = require("tactus.io.csv")
local json = require("tactus.io.json")
local file = require("tactus.io.file")

Procedure {
    input = {
    },
    output = {
            records_processed = field.number{required = true},
            summary = field.string{required = true}
    },
    function(input)

        -- Read CSV file (returns list of {header=value} dicts)
        -- Path is relative to the procedure file's directory
        local data = csv.read("data/sample.csv")

        -- Get record count
        local record_count = #data
        local high_performers = {}

        -- Iterate through records
        for i = 1, record_count do
            local row = data[i]
            local score = tonumber(row.score)
            if score >= 85 then
                -- Apply 10% bonus to high performers
                table.insert(high_performers, {
                    name = row.name,
                    original_score = score,
                    bonus_score = math.floor(score * 1.1),
                    category = row.category
                })
            end
        end

        Log.info("Loaded CSV data", {count = record_count})
        Log.info("Found high performers", {count = #high_performers})

        -- Write results to CSV (output/ folder is gitignored)
        csv.write("output/high_performers.csv", high_performers)

        -- Write summary to JSON
        local summary_data = {
            total_records = record_count,
            high_performers = #high_performers,
            processed_at = os.date()
        }
        json.write("output/summary.json", summary_data)

        -- Write raw text summary
        local summary_text = string.format(
            "Processed %d records, found %d high performers",
            record_count, #high_performers
        )
        file.write("output/summary.txt", summary_text)

        return {
            records_processed = record_count,
            summary = summary_text
        }
    end
}

Specification([[
Feature: File IO Operations
  Demonstrate reading and writing various file formats

  Scenario: Process CSV data and write results
    Given the procedure has started
    When the procedure runs
    Then the procedure should complete successfully
    And the output records_processed should be 5
]])

Quick Start

Run the example:

$tactus run 03-standard-library/02-file-io.tac

Test with mocks:

$tactus test 03-standard-library/02-file-io.tac --mock

View source on GitHub →

Explore more examples

Learn Tactus through practical, runnable examples organized by topic.

Part of the Anthus Platform
Tactus icon

Tactus

Tactus is a programming language and runtime for durable AI agent procedures with checkpointing, sandboxing, and built-in human-in-the-loop controls.

PART OF

The Anthus Platform

Solve complex business problems with AI and ML using a proven, reusable technology stack. These interoperable building blocks give our solutions a stronger operational foundation: durable procedures, MLOps control loops, workload orchestration, knowledge systems, observability, and programmable media workflows.

Plexus

MLOps platform for agent evaluation and iteration.

Tactus

Durable runtime for agent procedures.

Korporus

Agent operating system and federated shell.

Biblicus

Corpus analysis for extraction and retrieval.

Babulus

Marketing automation built around VideoML.

Kanbus

Durable multi-agent task management.

Caducus

Monitoring, alerts, and operator support.

Free and open-source softwareDesigned cybernetically by Ryan Porter
Contact us

How can we help?

GitHub

Browse the code.

LinkedIn

Company updates.

Discord

Join the chat.