3.1 General-Purpose Libraries
3.1.1 Global Utilities
3.1.2 Numbers
3.1.3 Strings
3.1.4 Booleans
3.1.5 option
3.1.6 either
3.1.7 pick
3.1.8 lists
3.1.9 sets
3.1.10 arrays
3.1.11 string-dict
3.1.12 Tables
3.1.13 gdrive-sheets
3.1.14 csv
3.1.15 color
3.1.16 The image libraries
3.1.17 reactors
3.1.18 chart
3.1.19 plot
3.1.20 statistics
3.1.21 math
3.1.22 matrices
3.1.23 Timing
3.1.24 fetch
3.1.25 filesystem
On this page:
read-file-string
write-file-string
stat
3.1.25 filesystem🔗

The filesystem library provides functions for reading and writing files, as well as working with paths.

These are selections from Alan Perlis’s Epigrams on Programming

The examples on this page work against a small filesystem that is set up every time the page loads. The filesystem contains the contents below, and the working directory is /.

/ ├── hello.txt "Hello, world!" ├── data/ │ ├── words "apple\nbanana\ncherry" │ ├── numbers.txt "1\n2\n3\n4\n5" │ └── story.txt "A long time ago in a galaxy far, far away..."

Usage:
include filesystem
import filesystem as ...

Examples:

import filesystem as FS check: FS.read-file-string("hello.txt") is "Hello, world!" words = FS.read-file-string("data/words") string-split-all(words, "\n") is [list: "apple", "banana", "cherry"] FS.read-file-string("does-not-exist") raises "No such file or directory" end

Reads the file at the given path and returns its contents as a String. Always assumes UTF-8 encoding.

Reports an error if the file is not found or is not readable.

write-file-string :: (
path :: String,
contents :: String
)

Writes the given contents the file at the given path. Always assumes UTF-8 encoding. If the file exists, it is overwritten, and if it does not exist, it is created.

Reports an error if the path refers to a non-existent directory, or if the file is present but not writable.

Examples:

import filesystem as FS check: FS.read-file-string("goodbye.txt") raises "No such file or directory" FS.write-file-string("goodbye.txt", "Until next time!") FS.read-file-string("goodbye.txt") is "Until next time!" FS.write-file-string("goodbye.txt", "See ya!") FS.read-file-string("goodbye.txt") is "See ya!" end

stat :: (
path :: String
)

There is also an optional field called native that may have additional information sometimes, depending on the platform. The three fields listed are guaranteed to be present across all platforms.

Returns an object with statistics about the file: its modified time (mtime), its creation time (ctime), and its size in bytes (size).

Examples:

import filesystem as FS check: test-start-time = time-now() # gives the current time in milliseconds FS.write-file-string("fresh-file.txt", "Brand new!") stats = FS.stat("fresh-file.txt") spy: stats end stats.size is string-length("Brand new!") # these tests are just indicating that the file was created and modified after # the test started stats.mtime is%(_ >= _) test-start-time stats.ctime is%(_ >= _) test-start-time end