Modules providing file related functions.
In order to use the functions provided by this module, you need to import this module:
>>> import Files
cd(path?)
Changes the current directory to the specified path.
path
: Path to the directory to change.The current directory or throws an exception if the directory does not exists
>>> cd
# cd
out = "/code/kalk/tests"
>>> mkdir "testdir"
>>> cd "testdir"
# cd("testdir")
out = "/code/kalk/tests/testdir"
>>> cd ".."
# cd("..")
out = "/code/kalk/tests"
>>> rmdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = false
dir(path?,recursive?)
List files and directories from the specified path or the current directory.
path
: The specified directory or the current directory if not specified.recursive
: A boolean to perform a recursive list. Default is false
.An enumeration of the files and directories.
>>> mkdir "testdir"
>>> cd "testdir"
# cd("testdir")
out = "/code/kalk/tests/testdir"
>>> mkdir "subdir"
>>> save_text("content", "file.txt")
>>> dir "."
# dir(".")
out = ["./file.txt", "./subdir"]
>>> save_text("content", "subdir/file2.txt")
>>> dir(".", true)
# dir(".", true)
out = ["./file.txt", "./subdir", "./subdir/file2.txt"]
>>> cd ".."
# cd("..")
out = "/code/kalk/tests"
>>> rmdir("testdir", true)
dir_exists(path)
Checks if the specified directory path exists on the disk.
path
: Path to a directory.true
if the specified directory path exists on the disk.
>>> mkdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = true
>>> rmdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = false
file_exists(path)
Checks if the specified file path exists on the disk.
path
: Path to a file.true
if the specified file path exists on the disk.
>>> rm "test.txt"
>>> file_exists "test.txt"
# file_exists("test.txt")
out = false
>>> save_text("content", "test.txt")
>>> file_exists "test.txt"
# file_exists("test.txt")
out = true
load_bytes(path)
Loads the specified file as binary.
path
: Path to a file to load as binary.The file loaded as a a byte buffer.
>>> load_bytes "test.csv"
# load_bytes("test.csv")
out = bytebuffer([97, 44, 98, 44, 99, 10, 49, 44, 50, 44, 51, 10, 52, 44, 53, 44, 54])
>>> ascii out
# ascii(out)
out = "a,b,c\n1,2,3\n4,5,6"
load_lines(path,encoding?)
Load each lines from the specified file path.
path
: Path to a file to load lines from.encoding
: The encoding of the file. Default is "utf-8"An enumeration on the lines.
>>> load_lines "test.csv"
# load_lines("test.csv")
out = ["a,b,c", "1,2,3", "4,5,6"]
load_text(path,encoding?)
Loads the specified file as text.
path
: Path to a file to load as text.encoding
: The encoding of the file. Default is "utf-8"The file loaded as a string.
>>> load_text "test.csv"
# load_text("test.csv")
out = "a,b,c\n1,2,3\n4,5,6"
mkdir(path)
Creates a directory at the specified path.
path
: Path of the directory to create.>>> mkdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = true
>>> rmdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = false
pwd
Gets the current directory.
The current directory.
>>> pwd
# pwd
out = "/code/kalk/tests"
rm(path)
Deletes a file from the specified path.
path
: Path to the file to delete.>>> rm "test.txt"
>>> file_exists "test.txt"
# file_exists("test.txt")
out = false
>>> save_text("content", "test.txt")
>>> file_exists "test.txt"
# file_exists("test.txt")
out = true
rmdir(path)
Deletes the directory at the specified path.
path
: Path to the directory to delete.>>> mkdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = true
>>> rmdir "testdir"
>>> dir_exists "testdir"
# dir_exists("testdir")
out = false
save_bytes(data,path)
Saves a byte buffer to the specified file path.
data
: The data to save.path
: Path to the file to save the data to.>>> utf8("Hello World!")
# utf8("Hello World!")
out = bytebuffer([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
>>> save_bytes(out, "test.bin")
>>> load_bytes("test.bin")
# load_bytes("test.bin")
out = bytebuffer([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
>>> utf8(out)
# utf8(out)
out = "Hello World!"
save_lines(lines,path,encoding?)
Saves an array of data as string to the specified files.
lines
: An array of data.path
: Path to the file to save the lines to.encoding
: The encoding of the file. Default is "utf-8">>> save_lines(1..10, "lines.txt")
>>> load_lines("lines.txt")
# load_lines("lines.txt")
out = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
save_text(text,path,encoding?)
Saves a text to the specified file path.
text
: The text to save.path
: Path to the file to save the text to.encoding
: The encoding of the file. Default is "utf-8">>> save_text("Hello World!", "test.txt")
>>> load_text("test.txt")
# load_text("test.txt")
out = "Hello World!"