os and io Libraries
Access system time, environment variables, file system, and stdin/stdout.
os and io Libraries is a free Lua Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.
The os Module Overview
The os module provides access to operating system features: time, date, environment variables, process control, and file system operations.
os.time and os.clock
os.time() returns the current time as a Unix timestamp. os.clock() returns CPU time used by the program — useful for benchmarking.
local t0 = os.clock()
for i = 1, 1e6 do end
print(("%.4f sec CPU"):format(os.clock() - t0))os.date
os.date(format, time) formats a timestamp. Use "*t" to get a table with year, month, day, hour, min, sec fields.
print(os.date("%Y-%m-%d %H:%M:%S")) -- 2025-01-15 14:30:00
local d = os.date("*t")
print(d.year, d.month, d.day)os.getenv
os.getenv(name) reads an environment variable. Returns nil if not set.
local path = os.getenv("PATH")
print(path and path:sub(1, 50) or "not set")os.execute and os.exit
os.execute(cmd) runs a shell command and returns the exit code. os.exit(code) terminates the process.
local ok = os.execute("echo hello")
print("exit status:", ok)os.rename and os.remove
os.rename(old, new) renames/moves a file. os.remove(path) deletes a file or empty directory. Both return nil plus error on failure.
io Module Overview
The io module provides file I/O. Files are opened with io.open, read with file:read, and written with file:write.
io.lines
io.lines(filename) returns an iterator over the lines of a file. Automatically closes the file when done.
for line in io.lines("data.txt") do
print(line)
endio.read from stdin
io.read() reads a line from standard input. io.read("*n") reads a number, io.read("*a") reads all of stdin.
io.write("Enter name: ")
local name = io.read()
print("Hello, " .. name)io.popen
io.popen(cmd, mode) opens a pipe to/from a shell command. Read the command's output like a file handle.
local f = io.popen("date")
local date = f:read("*l")
f:close()
print(date)stdin, stdout, stderr
io.stdin, io.stdout, io.stderr are pre-opened file handles for standard streams.
io.stderr:write("Error: something went wrong\n")os/io Question
Which function returns the CPU time used by the program?
Recap: os and io Libraries
The os library handles time, environment, processes, and file system ops. The io library handles file I/O, pipes, and standard streams. Together they cover most system interaction needs.
Learn Lua with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “os and io Libraries” lesson free?
Yes — any 3 lessons of the Lua Academy learning path, including “os and io Libraries”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.
What will I learn in “os and io Libraries”?
Access system time, environment variables, file system, and stdin/stdout. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “os and io Libraries” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- math Library Functions
- table Library Functions
- os and io Libraries
- The debug Library