IO
This page documents the changes & additions to the io
library in Pluto, which is built on top of Lua 5.4's.
io.rename
A variant of Lua's os.rename
that raises an error on failure and instead has no return values.
io.remove
A variant of Lua's os.remove
that raises an error on failure and instead has no return values.
io.copy
Copy a file to another file, creating a new file if needed.
Parameters
- A string path or file stream.
- A path towards the file to copy into.
Returns
A boolean indicating if the file was successfully copied.
if io.copy("./cfg/config.txt", "./backup_cfg/config.txt") then
print("Successfully created a backup config!")
else
print("Failed to create a backup config.")
end
io.part
Extracts the given part from a path.
Parameters
- A string path or file stream.
- The part to return, "parent" or "name".
Returns
The extracted part.
print(io.part("/path/to/foo.txt", "parent")) -- "/path/to"
print(io.part("/path/to/foo.txt", "name")) -- "foo.txt"
io.isdir
Parameters
- A string path or file stream.
Returns
A boolean indicating if the path led toward a directory.
local path = "./dir/main/"
local bool = io.isdir(path)
if bool then
print("Directory!")
else
print("Not a directory!")
end
io.isfile
Parameters
- A string path or file stream.
Returns
A boolean indicating if the path led towards a file.
local path = "./isfile/file"
local bool = io.isfile(path)
if bool then
print("File!")
else
print("Not a file!")
end
io.exists
Parameters
- A string path or file stream.
Returns
A boolean indicating if the path led towards an existing file or directory.
if io.exists("cfg/config.txt") then
print("Config exists!")
else
print("Config does not exist!")
end
io.makedir
Create a directory.
Parameters
- The path for the new directory.
Returns
A boolean indicating if the directory was successfully created.
if io.makedir("./cfg") then
print("Created cfg directory.")
else
print("Failed to create cfg directory.")
end
io.listdir
List all the files within a directory.
Parameters
- A string path to the directory.
- A boolean indicating whether or not to recurse sub-directories.
Returns
An index-based table containing file paths.
for _, filepath in io.listdir(".") do
print(filepath)
end
io.makedirs
Creates a directory and all the non-existing parent directories in the given path.
Parameters
- The string path for your new directory.
io.makedirs("A/B/C") -- Creates "C:\Users\Username\Desktop\Project\A\B\C"
io.absolute
Convert a relative path into an absolute one.
Parameters
- A string path or file stream.
- An optional bool if the path should be canonicalized. A canonical path does not go through symlinks.
Returns
A string representing the new file path.
io.absolute("cfg/cfg.txt") -- "C:\Users\Username\Desktop\Project\cfg\cfg.txt"
io.relative
Convert an absolute path into a relative one.
Parameters
- A string path or file stream.
Returns
A string representing the new file path.
io.relative(io.absolute("cfg/cfg.txt")) -- "cfg\cfg.txt"
io.contents
Get or set a file's contents.
Parameters
- A string path or file stream.
- The desired content. If this parameter is absent, this function works as a getter.
Returns
If this function is acting as a getter, it will return the file's contents or nil
if there was an error.
local text = io.contents("foo.txt") or ""
text ..= "bar"
io.contents("foo.txt", text)
This function is not only easier to use than the traditional io.open
approach, it also performs significantly better.
io.filesize
Fetch the size of a file in bytes.
Parameters
- A string path or file stream.
if io.filesize("cfg/config.txt") <= 1 then
print("Config is too small or empty.")
end
io.writetime
Get or set a file's modification time.
Parameters
- A string path or file stream.
- The desired timestamp. If this parameter is absent, this function works as a getter.
Returns
If this function is acting as a getter, it will return the file's last write time as seconds since the UNIX epoch.
local time = io.writetime("foo.txt")
time += 3600
io.writetime("foo.txt", time)
This example adds an hour (= 60 minutes = 3600 seconds) to the last write time of "foo.txt" (or errors if that file does not exist).
io.currentdir
Get or set the current working directory.
Parameters
- A path to the desired current working directory. If this parameter is absent, this function works as a getter.
Returns
If this function is acting as a getter, it will return the current working directory.
local cwd = io.currentdir() -- Get cwd
io.currentdir("abc/abc") -- Set a new cwd