fs/file
Reading and writing files, file information, copying and moving, and MD5 and SHA digests.
import fs.file
The fs.file package is the fullest set of tools for working with files. For settings and anything else that matters, use AtomicWrite: it writes to a temporary file and then replaces the original, so an interrupted program never leaves a half written file behind.
Types
FileInfo
struct FileInfo| Name | Type | Description |
|---|---|---|
size | i64 = 0 | The size in bytes. |
mod_time | i64 = 0 | When it was last changed. |
accessed_time | i64 = 0 | When it was last read. |
created_time | i64 = 0 | When it was created. |
permissions | int = 0 | The permissions, as octal digits. |
exists | bool = false | True when the file exists. |
import fs.file
func main:
file.WriteText("picture.png", "PNG")
info := file.Stat("picture.png")
println info.exists, info.size, info.mod_time > 0, info.permissions > 0
println file.Size("picture.png"), file.Exists("picture.png")
println file.ModTime("picture.png") > 0, file.AccessedTime("picture.png") > 0, file.CreatedTime("picture.png") >= 0
file.Chmod("picture.png", 0o600)
println file.Permissions("picture.png")
println file.Extension("picture.png"), file.MimeType("picture.png"), file.MimeType("letter.pdf")
println file.Stat("missing.txt").exists
endFunctions
Open
func Open(path: str, mode: str): File*import fs.file
func main:
file.WriteText("poem.txt", "The children of Adam are limbs of one body\n")
file.Append("poem.txt", "having been created of one essence\n")
print file.ReadText("poem.txt")
println file.ReadAll("poem.txt") == file.ReadText("poem.txt")
lines := file.ReadLines("poem.txt")
println lines.len(), lines.get(1)
file.WriteBytes("data.bin", "ABCDEF")
println file.ReadBytes("data.bin", 3)
file.AtomicWrite("settings.txt", "language=english")
println file.ReadText("settings.txt")
f := file.Open("poem.txt", "rb")
println f.readline()
file.Close(f)
endClose
func Close(f: File*)The example for this item is shown with the ones above: fs-file-ReadText.salam
ReadText
func ReadText(path: str): strThe example for this item is shown with the ones above: fs-file-ReadText.salam
ReadAll
func ReadAll(path: str): strThe example for this item is shown with the ones above: fs-file-ReadText.salam
ReadBytes
func ReadBytes(path: str, n: int): strn bytes of the file.The example for this item is shown with the ones above: fs-file-ReadText.salam
ReadLines
func ReadLines(path: str): Vector<str>The example for this item is shown with the ones above: fs-file-ReadText.salam
WriteText
func WriteText(path: str, data: str): intThe example for this item is shown with the ones above: fs-file-ReadText.salam
WriteBytes
func WriteBytes(path: str, data: str): intThe example for this item is shown with the ones above: fs-file-ReadText.salam
Append
func Append(path: str, data: str): intThe example for this item is shown with the ones above: fs-file-ReadText.salam
AtomicWrite
func AtomicWrite(path: str, data: str): intThe example for this item is shown with the ones above: fs-file-ReadText.salam
Size
func Size(p: str): i64The example for this item is shown with the ones above: fs-file-Stat.salam
ModTime
func ModTime(p: str): i64The example for this item is shown with the ones above: fs-file-Stat.salam
AccessedTime
func AccessedTime(p: str): i64The example for this item is shown with the ones above: fs-file-Stat.salam
CreatedTime
func CreatedTime(p: str): i64The example for this item is shown with the ones above: fs-file-Stat.salam
Permissions
func Permissions(p: str): intThe example for this item is shown with the ones above: fs-file-Stat.salam
Chmod
func Chmod(p: str, mode: int): int0o600.The example for this item is shown with the ones above: fs-file-Stat.salam
Extension
func Extension(p: str): strThe example for this item is shown with the ones above: fs-file-Stat.salam
MimeType
func MimeType(p: str): strThe example for this item is shown with the ones above: fs-file-Stat.salam
Stat
func Stat(p: str): FileInfoThe example for this item is shown with the ones above: fs-file-Stat.salam
Copy
func Copy(src: str, dst: str): intimport fs.file
func main:
file.WriteText("original.txt", "hello")
file.Copy("original.txt", "copy.txt")
file.Move("copy.txt", "moved.txt")
file.Rename("moved.txt", "final.txt")
println file.Exists("copy.txt"), file.Exists("final.txt"), file.ReadText("final.txt")
file.Delete("final.txt")
println file.Exists("final.txt")
endMove
func Move(src: str, dst: str): intThe example for this item is shown with the ones above: fs-file-Copy.salam
Rename
func Rename(src: str, dst: str): intThe example for this item is shown with the ones above: fs-file-Copy.salam
Delete
func Delete(p: str): intThe example for this item is shown with the ones above: fs-file-Copy.salam
Exists
func Exists(p: str): boolThe example for this item is shown with the ones above: fs-file-Stat.salam
Md5Hex
func Md5Hex(p: str): strimport fs.file
func main:
file.WriteText("message.txt", "abc")
println file.Md5Hex("message.txt")
println file.Sha1Hex("message.txt")
println file.Sha256Hex("message.txt")
println file.Sha512Hex("message.txt").len()
endSha1Hex
func Sha1Hex(p: str): strThe example for this item is shown with the ones above: fs-file-Sha256Hex.salam
Sha256Hex
func Sha256Hex(p: str): strThe example for this item is shown with the ones above: fs-file-Sha256Hex.salam
Sha512Hex
func Sha512Hex(p: str): strThe example for this item is shown with the ones above: fs-file-Sha256Hex.salam