File reading and writing
Version 0.9.0
Chadnaut 2024
https://github.com/Chadnaut/Attract-Mode-Modules
::fe.load_module("fs");
// read a file line-by-line
local file = ::fs.open(FeConfigDirectory + "attract.cfg", "r");
local line = null;
while (line = file.read_line()) print(line + "\n");
// read a dir
local dir = ::fs.readdir(FeConfigDirectory, true);
foreach (d in dir) print(d + "\n")::fs.open(path, mode = "r")FileHander - Returns a FileHander.::fs.copy(from, to, overwrite?)bool - Copy file.::fs.move(from, to, overwrite?)bool - Move file.::fs.unlink(path)bool - Delete file.::fs.rename(from, to)bool - Rename file.::fs.exists(path)bool - Return true if path exists.::fs.file_exists(path)bool - Return true if file exists.::fs.directory_exists(path)bool - Return true if directory exists.::fs.crc32(path)int - Return unsigned CRC32 of file.::fs.readdir(path, absolute = false)[string] - Returns a list of items in path.::fs.join(...)string - Joins passed arguments using slash/delimiter.::fs.add_trailing_slash(path)string - Ensure path has trailing slash.::fs.remove_trailing_slash(path)string - Ensure path has no trailing.
Note that functions will throw errors on invalid actions, such as copying a file that doesn't exist.
These shortcuts are provided to make it easier to perform simple tasks.
::fs.file_size(path)int - Shortcut forFileHander.len.::fs.read(path)string - Shortcut forFileHander.read.::fs.read_lines(path)[string] - Shortcut forFileHander.read_lines.::fs.read_csv(path)[[string]] - Shortcut forFileHander.read_csv.::fs.read_pairs(path)[[string]] - Shortcut forFileHander.read_pairs.::fs.write(path, string)- Shortcut forFileHander.write.::fs.write_lines(path, lines)- Shortcut forFileHander.write_lines.::fs.write_csv(path, csv)- Shortcut forFileHander.write_csv.::fs.write_pairs(path, pairs)- Shortcut forFileHander.write_pairs.
| Flag | Name | Read | Write | Create | Truncate | Append |
|---|---|---|---|---|---|---|
r |
Read | ✔ | ❌ | ❌ | ❌ | ❌ |
r+ |
Read+ | ✔ | ✔ | ❌ | ❌ | ❌ |
w |
Write | ❌ | ✔ | ✔ | ✔ | ❌ |
w+ |
Write+ | ❌ | ✔ | ✔ | ✔ | ❌ |
a |
Append | ❌ | ✔ | ✔ | ❌ | ✔ |
a+ |
Append+ | ✔ | ✔ | ✔ | ❌ | ✔ |
b |
Binary | * | * | * | * | * |
- Flags that
createfiles will do so onopen(). - Calling methods on a
FileHandlerthat does not support it will returnnulland/or print an error inlast_run.log. r+will begin writing at the head of the file, useamode, orend()to move the pointer.- Add
bto any flag (ie:rb,wb) to operate in binary mode.
The FileHander should be used to process file data one line at a time, which is more efficient than reading the entire file and looping through the results.
read_line()string - Return next line in file, or null if none.read_lines()[string] - Return all lines as array.read()string - Return entire file as string.write_line(string)- Write string to file with new line after.write_lines([string])- Write array of strings to file with new lines after.write(string)- Write string to entire file.read_csv_line(delim = ";")[string] - Return next csv line as array, or null if none.read_csv(delim = ";")[[string]] - Return all csv lines as an array of arrays.write_csv_line(row, delim = ";")- Write delimited values to file.write_csv(csv, delim = ";")- Write array of delimited values to file.read_pairs_line()[string] - Return next pairs line as array, or null if none.read_pairs()[[string]] - Return all pairs lines as an array of arrays.write_pairs_line(row)- Write pair values to file.write_pairs(pairs)- Write array of pair values to file.close()- Closes the file, and allow other handlers to open it.len()int - Return length of file.exists()bool - Return true if file exists.rewind()- Place pointer at start of file.end()- Place pointer at end of file.seek(offset, origin?)- Move pointer within file.offset- number of characters to move (NOTE:\n== 2 chars).origin- offset from beginningb, ende, or currentc.
