Turtle

Library › system — the command line, environment and files

system — the command line, environment and files

import system

The command line, environment and files: read arguments and environment variables, check and copy files, and pack or unpack archives.

Functions

args, exists, isfile, isfolder, exit, env, scriptfolder, contents, erase, warn, copyto, moveto, makefolder, walk, pack, unpack, loadenv, options, memory, sizeof, freememory.

args[]

The words typed after the script's name: turtle report.turtle a.txt b.txt Gives back: a list of text (empty if none).

Example

show args[] .

Output

[]

exists[path]

Is there a file or folder at path?

Example

show exists["notes.txt"] .

Output

false

isfile[path]

Is path a file?

Example

show isfile["notes.txt"] .

Output

false

isfolder[path]

Is path a folder?

Example

makefolder["out"]
show isfolder["out"] .

Output

true

exit[code]

Ends the program now. code is optional: 0 (the default) means it went well, anything else that it failed.

Example

exit[1]

This example stops with an error on purpose or when run alone.

env[name]

An environment variable's value.

Example

import system
home = env["HOME"]
show home .

Output

/Users/nigy

The output is your own home folder, so it differs between people and machines.

scriptfolder[]

The full path of the folder the running script is in.

Example

import system
data = "{scriptfolder[]}/data.csv"
show data .

Output

/private/tmp/claude-501/-Users-nigy-Desktop-turtle/00572d6e-9342-4345-a34d-34b881f3a559/scratchpad/libfix/w/data.csv

The output is the folder the program is in, so it differs on each computer.

contents[path]

The names of the files and folders in a folder (path is optional, "." by default).

Example

makefolder["out"]
[write] out/a.txt
"one"
[end]
show contents["out"] .

Output

[ "a.txt" ]

erase[path]

Deletes a file, or a folder and everything in it.

Example

makefolder["out"]
erase["out"]
show exists["out"] .

Output

false

warn ... .

Like show, but to standard error, so it isn't mixed with output that's piped or saved.

Example

import system
name = "report.csv"
warn "can't read ", name .

Output

can't read report.csv

copyto[from, to, replace]

Copies a file, or a folder and everything in it, to the path to (missing folders on the way are made). replace optional: true replaces what's already at to; without it, something there is an error

Example

[write] report.csv
"data"
[end]
copyto["report.csv", "backup/report.csv"]
show exists["backup/report.csv"] .

Output

true

moveto[from, to, replace]

Moves (or renames) a file or folder; replace as for copyto.

Example

[write] old.txt
"data"
[end]
moveto["old.txt", "archive/old.txt"]
show exists["archive/old.txt"] .

Output

true

makefolder[path]

Makes a folder, and any missing folders above it.

Example

show makefolder["out/2026/october"] .

Output

none

walk[folder]

Every file under the folder, in its subfolders too, sorted; each path starts with folder and uses /. "." if left out.

Example

makefolder["src"]
[write] src/a.txt
"text"
[end]
[loop][f in walk["src"]]
    show f .
[loop][end]

Output

src/a.txt

pack[from, archive, replace]

Puts a file, or a folder and everything in it, into an archive.

Example

makefolder["src"]
[write] src/a.txt
"text"
[end]
pack["src", "src.zip"]
unpack["src.zip", "restored"]
show walk["restored"] .

Output

[ "restored/src/a.txt" ]

unpack[archive, folder, replace]

Puts an archive's files into folder.

Example

makefolder["src"]
[write] src/a.txt
"text"
[end]
pack["src", "src.zip"]
unpack["src.zip", "restored"]
show walk["restored"] .

Output

[ "restored/src/a.txt" ]

loadenv[file]

Reads a .env file (".env" if left out): KEY=value lines, # comments, "quoted" or 'quoted' values.

Example

[write] app.env
"KEY=value"
[end]
settings = loadenv["app.env"]
show settings at get "KEY" .

Output

value

options[name: default, ...]

Named options from the command line.

Example

import system
opts = options["--out": "result.csv", "-v": false]
out = opts at get["--out"]
show out .

Output

result.csv

memory[]

How much memory the program's values hold now, in bytes.

Example

show memory[] / 1048576, " MB" .

Output

0.44379425048828125 MB

sizeof[x]

About how many bytes x takes, with everything in it (an estimate: a value inside it twice counts once).

Example

import system
import test
rows = list [1, 2, 3]
size = sizeof[rows]
show size > 0 .

Output

true

freememory[]

Collects the values nothing refers to now, and gives that memory back to the system: worth it between big steps, not after every small one.

Example

import system
rows = none
freed = freememory[]
show freed .

Output

154656

The output is the memory free right now, so it differs on each run and machine.

Top