Standard library · System and tools

fs/file

Reading and writing files, file information, copying and moving, and MD5 and SHA digests.

import fs.file
On this page
  1. Types
  2. Functions

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
Everything about a file.
NameTypeDescription
sizei64 = 0The size in bytes.
mod_timei64 = 0When it was last changed.
accessed_timei64 = 0When it was last read.
created_timei64 = 0When it was created.
permissionsint = 0The permissions, as octal digits.
existsbool = falseTrue when the file exists.
fs-file-Stat.salam
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
end
Outputtrue 3 true true 3 true true true true 600 .png image/png application/pdf false

Functions

Open

func Open(path: str, mode: str): File*
Opens a file to work through it piece by piece; the modes are the same as os.Open.
fs-file-ReadText.salam
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)
end
OutputThe children of Adam are limbs of one body having been created of one essence true 2 having been created of one essence ABC language=english The children of Adam are limbs of one body

Close

func Close(f: File*)
Closes an open file.

The example for this item is shown with the ones above: fs-file-ReadText.salam

ReadText

func ReadText(path: str): str
Reads the whole file as text.

The example for this item is shown with the ones above: fs-file-ReadText.salam

ReadAll

func ReadAll(path: str): str
Reads the whole file; it suits binary files too.

The example for this item is shown with the ones above: fs-file-ReadText.salam

ReadBytes

func ReadBytes(path: str, n: int): str
Reads the first n 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>
Returns the file's lines in a vector.

The example for this item is shown with the ones above: fs-file-ReadText.salam

WriteText

func WriteText(path: str, data: str): int
Writes text to a file, replacing what was there.

The example for this item is shown with the ones above: fs-file-ReadText.salam

WriteBytes

func WriteBytes(path: str, data: str): int
Writes binary data to a file.

The example for this item is shown with the ones above: fs-file-ReadText.salam

Append

func Append(path: str, data: str): int
Adds to the end of a file.

The example for this item is shown with the ones above: fs-file-ReadText.salam

AtomicWrite

func AtomicWrite(path: str, data: str): int
Writes a file safely and all at once: either the complete new version survives or the old one does, never a half written file.

The example for this item is shown with the ones above: fs-file-ReadText.salam

Size

func Size(p: str): i64
The size of a file in bytes.

The example for this item is shown with the ones above: fs-file-Stat.salam

ModTime

func ModTime(p: str): i64
When it was last changed.

The example for this item is shown with the ones above: fs-file-Stat.salam

AccessedTime

func AccessedTime(p: str): i64
When it was last read.

The example for this item is shown with the ones above: fs-file-Stat.salam

CreatedTime

func CreatedTime(p: str): i64
When it was created.

The example for this item is shown with the ones above: fs-file-Stat.salam

Permissions

func Permissions(p: str): int
The permissions, as octal digits.

The example for this item is shown with the ones above: fs-file-Stat.salam

Chmod

func Chmod(p: str, mode: int): int
Changes the permissions; give the value in octal, such as 0o600.

The example for this item is shown with the ones above: fs-file-Stat.salam

Extension

func Extension(p: str): str
The file's extension, with its dot.

The example for this item is shown with the ones above: fs-file-Stat.salam

MimeType

func MimeType(p: str): str
The file's MIME type from its extension, for serving it over the web.

The example for this item is shown with the ones above: fs-file-Stat.salam

Stat

func Stat(p: str): FileInfo
Everything about a file at once.

The example for this item is shown with the ones above: fs-file-Stat.salam

Copy

func Copy(src: str, dst: str): int
Copies a file.
fs-file-Copy.salam
import 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")
end
Outputfalse true hello false

Move

func Move(src: str, dst: str): int
Moves a file.

The example for this item is shown with the ones above: fs-file-Copy.salam

Rename

func Rename(src: str, dst: str): int
Renames a file.

The example for this item is shown with the ones above: fs-file-Copy.salam

Delete

func Delete(p: str): int
Deletes a file.

The example for this item is shown with the ones above: fs-file-Copy.salam

Exists

func Exists(p: str): bool
True when the file exists.

The example for this item is shown with the ones above: fs-file-Stat.salam

Md5Hex

func Md5Hex(p: str): str
The MD5 digest of the file's content; for a quick comparison only, not for security.
fs-file-Sha256Hex.salam
import 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()
end
Output900150983cd24fb0d6963f7d28e17f72 a9993e364706816aba3e25717850c26c9cd0d89d ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad 128

Sha1Hex

func Sha1Hex(p: str): str
The SHA-1 digest of the file's content.

The example for this item is shown with the ones above: fs-file-Sha256Hex.salam

Sha256Hex

func Sha256Hex(p: str): str
The SHA-256 digest of the file's content, for checking that a download arrived intact.

The example for this item is shown with the ones above: fs-file-Sha256Hex.salam

Sha512Hex

func Sha512Hex(p: str): str
The SHA-512 digest of the file's content.

The example for this item is shown with the ones above: fs-file-Sha256Hex.salam