IO Functions
New functions for the io
library, implemented by Pluto.
io.isdir
Parameters
- A string path or file stream.
Returns
A boolean indicating if the path led toward a directory.
Example
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.
Example
local path = "./isfile/file"
local bool = io.isdir(path)
if bool then
print("File!")
else
print("Not a file!")
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
A table mapping indices to file paths.
Example
for _, filepath in io.listdir(".") do
print(filepath)
end
io.exists
Parameters
- A string path or file stream.
Returns
A boolean indicating if the path led towards an existing file or directory.
Example
if io.exists("cfg/config.txt") then
print("Config exists!")
else
print("Config does not exist!")
end
io.copyto
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.
Example
if io.copyto("./cfg/config.txt", "./backup_cfg/config.txt") then
print("Successfully created a backup config!")
else
print("Failed to create a backup config.")
end
As of 0.3.2, this may throw an exception on failure.
io.filesize
Fetch the size of a file in kilobytes.
Parameters
- A string path or file stream.
Returns
A double.
Example
if io.filesize("cfg/config.txt") < 1.0 then
print("Config is too small or empty.")
end
As of 0.3.2, this may throw an exception on failure.
io.makedir
Create a directory.
Parameters
- The path for the new directory.
Returns
A boolean indicating if the directory was successfully created.
Example
if io.makedir("./cfg") then
print("Created cfg directory.")
else
print("Failed to create cfg directory.")
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.
Example
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.
Returns
A string representing the new file path.
Example
--> cfg/config.txt
io.absolute("cfg/cfg.txt") --> "C:\Users\Username\Desktop\Project\cfg\cfg.txt"