As we saw with "maths", modules of code can be imported to provide data and functions in addition to the built-in ones.
For example, the
os
module has a relation generator named
cwd
which returns the 'current working directory'.
import("os")
os.cwd
┌──────────┐ │ dirname │ ├──────────┤ │ localdir │ └──────────┘
There's also a function
os.file
which, given one or more
dirname
, returns the filenames in those directories.
So we can compose the two of them to get a list of the files in the current directory.
os.file(os.cwd)
┌─────────────────┬─────────────────────────┬───────┬──────┬────────┬─────┐ │ filename │ path │ ext │ size │ is_dir │ err │ ├─────────────────┼─────────────────────────┼───────┼──────┼────────┼─────┤ │ test.txt │ /localdir/test.txt │ .txt │ 49 │ false │ │ │ subdir │ /localdir/subdir │ │ 4096 │ true │ │ └─────────────────┴─────────────────────────┴───────┴──────┴────────┴─────┘
Futhermore, there's another function
os.file_content
which, given one or more file
path
, returns the contents of those files.
So we can compose all three, and we can use all our relational query features to get a list of selected files and their contents.
os.file_content(os.file(os.cwd)[not is_dir])
┌──────────┬──────┬──────┬────────┬─────┬────────┬────────────────────────────────────────────┐ │ filename │ ext │ size │ is_dir │ sep │ format │ r │ ├──────────┼──────┼──────┼────────┼─────┼────────┼────────────────────────────────────────────┤ │ test.txt │ .txt │ 49 │ false │ │ │ ┌─────┬──────────────────────────────────┐ │ │ │ │ │ │ │ │ │ seq │ ss │ │ │ │ │ │ │ │ │ ├─────┼──────────────────────────────────┤ │ │ │ │ │ │ │ │ │ 0 │ This is a test file │ │ │ │ │ │ │ │ │ │ 1 │ │ │ │ │ │ │ │ │ │ │ 2 │ It has several lines │ │ │ │ │ │ │ │ │ │ 3 │ of text. │ │ │ │ │ │ │ │ │ └─────┴──────────────────────────────────┘ │ └──────────┴──────┴──────┴────────┴─────┴────────┴────────────────────────────────────────────┘
The file contents are currently read into lines but in future we'll be able override that by passing a variety of
format
and
sep
options. For now we can re-combine the lines if we want to form a single block of file data, e.g. using
str_rel
.
os.file_content(os.file(os.cwd)[not is_dir])(str_rel){filename, s}
┌─────────────────────────┬──────────┐ │ s │ filename │ ├─────────────────────────┼──────────┤ │ This is a test file │ test.txt │ │ │ │ │ It has several lines │ │ │ of text. │ │ └─────────────────────────┴──────────┘
Or we can read in a whole binary file by using
format:="b"
.