KScript.IO Files and folders: read, write, list, move, copy and delete.
Import it at the top of a script:
.kscript XML Copy
<import package="KScript.IO" />.kscript XML Copy
import KScript.IOCommands Commands are called with @, in any expression: @name(arguments).
# @append_text (path: file_path , text )Adds text to the end of a file.
Parameter Takes path required file_path text required any value
Arguments can be named: @append_text(…, text: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('log.txt', 'one;'); @append_text('log.txt', 'two;')</do>
<echo>{ @read_text('log.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('log.txt', 'one;')
@append_text('log.txt', 'two;')
echo @read_text('log.txt')# @copy (from: file_path , to: file_path , overwrite?: bool ) → file Copies a file or folder, and returns the copy as a file value. to is a full path, or an existing folder to copy it into. overwrite: true replaces files that are already there.
Parameter Takes from required file_path to required file_path overwrite optional a flag: overwrite or not overwrite
Arguments can be named: @copy(…, to: …, overwrite: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('a.txt', 'a')</do>
<echo>{ @copy('a.txt', 'b.txt').name }</echo>.kscript XML Copy
import KScript.IO
@write_text('a.txt', 'a')
echo @copy('a.txt', 'b.txt').name# @create_folder (path: file_path ) → file Creates a folder (and any folders above it), and returns it as a file value. Does nothing if it is already there.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @create_folder('out/reports').name }</echo>.kscript XML Copy
import KScript.IO
echo @create_folder('out/reports').nameThe folder kscript was run from.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @cwd()->count() > 0 }</echo>.kscript XML Copy
import KScript.IO
echo @cwd()->count() > 0# @delete (path: file_path )Deletes a file, or a folder and everything in it. Does nothing if it isn't there.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('old.txt', 'x')</do>
<do>@delete('old.txt')</do>
<echo>{ @exists('old.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('old.txt', 'x')
@delete('old.txt')
echo @exists('old.txt')# @exists (path: file_path ) → bool True if a file or folder is at the path.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('a.txt', 'a')</do>
<echo>{ @exists('a.txt') } { @exists('b.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('a.txt', 'a')
echo "{ @exists('a.txt') } { @exists('b.txt') }"# @file (path: file_path ) → file A file value for a path, with fields like .name, .extension and .size, and methods like ->read_text().
Example
.kscript XML Copy
<import package="KScript.IO" />
<let name="report" value="@file('out/report.pdf')" />.kscript XML Copy
import KScript.IO
$report = @file('out/report.pdf')# @files (folder: file_path , pattern? , recursive?: bool ) → list The files in a folder, as file values. pattern is a wildcard like '*.txt'.
Parameter Takes folder required file_path pattern optional any value recursive optional a flag: recursive or not recursive
Arguments can be named: @files(…, pattern: …, recursive: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<for each="f" in="@files('.', '*.log')"><echo>{$f.name}</echo></for>.kscript XML Copy
import KScript.IO
for f in @files('.', '*.log') {
echo $f.name
}# @folders (folder: file_path , pattern? , recursive?: bool ) → list The folders in a folder, as file values.
Parameter Takes folder required file_path pattern optional any value recursive optional a flag: recursive or not recursive
Arguments can be named: @folders(…, pattern: …, recursive: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@create_folder('2025'); @create_folder('2026')</do>
<echo>{ @folders('.')->map(fn(f) => $f.name)->sort() }</echo>.kscript XML Copy
import KScript.IO
@create_folder('2025')
@create_folder('2026')
echo @folders('.')->map(fn(f) => $f.name)->sort()# @full_path (path: file_path ) → string The absolute path.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @full_path('notes.txt')->ends_with('notes.txt') }</echo>.kscript XML Copy
import KScript.IO
echo @full_path('notes.txt')->ends_with('notes.txt')The user's home folder.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @home()->count() > 0 }</echo>.kscript XML Copy
import KScript.IO
echo @home()->count() > 0# @is_file (path: file_path ) → bool True if a file is at the path.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('a.txt', 'a')</do>
<echo>{ @is_file('a.txt') } { @is_file('.') }</echo>.kscript XML Copy
import KScript.IO
@write_text('a.txt', 'a')
echo "{ @is_file('a.txt') } { @is_file('.') }"# @is_folder (path: file_path ) → bool True if a folder is at the path.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@create_folder('photos')</do>
<echo>{ @is_folder('photos') }</echo>.kscript XML Copy
import KScript.IO
@create_folder('photos')
echo @is_folder('photos')# @join_path (base , parts... ) → string Joins a folder and the parts below it with the right separator. Parts may be a list: @join_path(base: $folder, ['archive', $name]).
Arguments can be named: @join_path(…, parts: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @join_path('reports', '2026', 'november.pdf') }</echo>.kscript XML Copy
import KScript.IO
echo @join_path('reports', '2026', 'november.pdf')# @known_folder (name: home|desktop|documents|downloads|music|pictures|videos|appdata|temp ) → string A standard folder: home, desktop, documents, downloads, music, pictures, videos, appdata or temp.
Parameter Takes name required one of these words: home|desktop|documents|downloads|music|pictures|videos|appdata|temp
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @known_folder(downloads)->count() > 0 }</echo>.kscript XML Copy
import KScript.IO
echo @known_folder(downloads)->count() > 0# @move (from: file_path , to: file_path , overwrite?: bool ) → file Moves (or renames) a file or folder, and returns it as a file value. to is a full path, or an existing folder to move it into. overwrite: true replaces a file that is already there.
Parameter Takes from required file_path to required file_path overwrite optional a flag: overwrite or not overwrite
Arguments can be named: @move(…, to: …, overwrite: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('report.txt', 'Q3')</do>
<do>@create_folder('archive')</do>
<let name="file" value="@file('report.txt')" />
<do>@move($file, @join_path('archive', $file.name))</do>
<echo>{ @exists('archive/report.txt') } { @exists('report.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('report.txt', 'Q3')
@create_folder('archive')
$file = @file('report.txt')
@move($file, @join_path('archive', $file.name))
echo "{ @exists('archive/report.txt') } { @exists('report.txt') }"# @read_bytes (path: file_path ) → base64 Reads every byte of a file as a base64 value, including binary data.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('sample.txt', 'Hello')</do>
<echo>{ @read_bytes('sample.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('sample.txt', 'Hello')
echo @read_bytes('sample.txt')# @read_csv (path: file_path , header?: bool , as?: type ) → list Reads CSV as rows of maps (or rows of lists with header: false). An optional type validates the rows.
Parameter Takes path required file_path header optional a flag: header or not header as optional type
Arguments can be named: @read_csv(…, header: …, as: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_csv('people.csv', [{ name: 'Joan' }])</do>
<echo>{ @read_csv('people.csv')->first().name }</echo>.kscript XML Copy
import KScript.IO
@write_csv('people.csv', [{ name: 'Joan' }])
echo @read_csv('people.csv')->first().name# @read_env (path: file_path ) → map A .env file (NAME=value lines) read into a map. $ uses earlier lines or the environment.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('.env', 'PORT=8080' + @char_of(10) + 'URL=http://localhost:${PORT}')</do>
<echo>{ @read_env('.env').URL }</echo>.kscript XML Copy
import KScript.IO
@write_text('.env', 'PORT=8080' + @char_of(10) + 'URL=http://localhost:${PORT}')
echo @read_env('.env').URL# @read_json (path: file_path , as?: type ) → any A JSON file read into maps and lists. With as: a <type>, the data is checked and turned into records. A JSON file read into maps and lists. With as: a type, the data is checked and turned into records.
Arguments can be named: @read_json(…, as: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_json('people.json', [{ name: 'Joan', age: 82 }])</do>
<echo>{ @read_json('people.json')->first().name }</echo>.kscript XML Copy
import KScript.IO
@write_json('people.json', [{ name: 'Joan', age: 82 }])
echo @read_json('people.json')->first().name# @read_lines (path: file_path ) → list The lines of a file.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('todo.txt', 'milk' + @char_of(10) + 'eggs')</do>
<echo>{ @read_lines('todo.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('todo.txt', 'milk' + @char_of(10) + 'eggs')
echo @read_lines('todo.txt')# @read_text (path: file_path ) → string The text of a file. Relative paths are from the script's folder.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('config.json', '{ "port": 8080 }')</do>
<let name="settings" value="@read_text('config.json')->convert(from: json)" />
<echo>$settings.port</echo>.kscript XML Copy
import KScript.IO
@write_text('config.json', '{ "port": 8080 }')
$settings = @read_text('config.json')->convert(from: json)
echo $settings.port# @read_yaml (path: file_path ) → any A YAML file read into maps and lists.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('app.yaml', 'name: Shop' + @char_of(10) + 'port: 8080')</do>
<echo>{ @read_yaml('app.yaml').port }</echo>.kscript XML Copy
import KScript.IO
@write_text('app.yaml', 'name: Shop' + @char_of(10) + 'port: 8080')
echo @read_yaml('app.yaml').port# @temp_folder () → string The folder for temporary files.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @temp_folder()->count() > 0 }</echo>.kscript XML Copy
import KScript.IO
echo @temp_folder()->count() > 0# @with_temp_file (use: fn ) → any Calls a function with a temporary file, then removes the file when the call ends.
Parameter Takes use required fn
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @with_temp_file(fn(file) => @exists($file)) }</echo>.kscript XML Copy
import KScript.IO
echo @with_temp_file(fn(file) => @exists($file))# @with_temp_folder (use: fn ) → any Calls a function with a temporary folder, then removes it when the call ends.
Parameter Takes use required fn
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @with_temp_folder(fn(folder) => @exists($folder)) }</echo>.kscript XML Copy
import KScript.IO
echo @with_temp_folder(fn(folder) => @exists($folder))# @write_atomic (path: file_path , value )Replaces a file atomically with text or the bytes of a base64 value.
Parameter Takes path required file_path value required any value
Arguments can be named: @write_atomic(…, value: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_atomic('notes.txt', 'Saved')</do>
<echo>{ @read_text('notes.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_atomic('notes.txt', 'Saved')
echo @read_text('notes.txt')# @write_bytes (path: file_path , bytes: base64 )Writes the exact bytes held by a base64 value.
Arguments can be named: @write_bytes(…, bytes: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<let name="data" type="base64" value="'AAE='" />
<do>@write_bytes('sample.bin', $data)</do>
<echo>{ @read_bytes('sample.bin') }</echo>.kscript XML Copy
import KScript.IO
$data: base64 = 'AAE='
@write_bytes('sample.bin', $data)
echo @read_bytes('sample.bin')# @write_csv (path: file_path , rows: list , headers?: list )Writes maps as CSV with a header, or lists as rows. Optional headers select and order columns.
Arguments can be named: @write_csv(…, rows: …, headers: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_csv('people.csv', [{ name: 'Joan' }])</do>
<echo>{ @read_text('people.csv') }</echo>.kscript XML Copy
import KScript.IO
@write_csv('people.csv', [{ name: 'Joan' }])
echo @read_text('people.csv')# @write_json (path: file_path , value )Writes a value to a file as indented JSON.
Parameter Takes path required file_path value required any value
Arguments can be named: @write_json(…, value: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_json('settings.json', { theme: 'dark', size: 14 })</do>
<echo>{ @read_text('settings.json') }</echo>.kscript XML Copy
import KScript.IO
@write_json('settings.json', { theme: 'dark', size: 14 })
echo @read_text('settings.json')# @write_text (path: file_path , text )Writes text to a file, replacing it. Folders are created as needed.
Parameter Takes path required file_path text required any value
Arguments can be named: @write_text(…, text: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('hello.txt', 'Hello!')</do>
<echo>{ @read_text('hello.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('hello.txt', 'Hello!')
echo @read_text('hello.txt')# @write_yaml (path: file_path , value )Writes a value to a file as YAML.
Parameter Takes path required file_path value required any value
Arguments can be named: @write_yaml(…, value: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_yaml('settings.yaml', { theme: 'dark', size: 14 })</do>
<echo>{ @read_text('settings.yaml') }</echo>.kscript XML Copy
import KScript.IO
@write_yaml('settings.yaml', { theme: 'dark', size: 14 })
echo @read_text('settings.yaml')Methods On file values: ->read_text() , ->read_lines() , ->join() , ->relative_to() , ->read_bytes() , ->read_json() , ->sha256() , ->write_text() , ->copy_to() , ->move_to() , ->delete()