1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435 | Path.accessed:
short: access time
description: >
Gets the file access time of a file.
return:
type: 'Int64?'
description: >
A 64-bit unix epoch timestamp representing when the file or directory was last
accessed, or `none` if no such file or directory exists.
args:
path:
type: 'Path'
description: >
The path of the file whose access time you want.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./file.txt).accessed() == Int64(1704221100)
assert (./not-a-file).accessed() == none
Path.append:
short: append to a file
description: >
Appends the given text to the file at the specified path, creating the file if
it doesn't already exist. Failure to write will result in a runtime error.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path of the file to append to.
text:
type: 'Text'
description: >
The text to append to the file.
permissions:
type: 'Int32'
default: 'Int32(0o644)'
description: >
The permissions to set on the file if it is being created.
example: |
(./log.txt).append("extra line\n")!
Path.append_bytes:
short: append bytes to a file
description: >
Appends the given bytes to the file at the specified path, creating the file if
it doesn't already exist. Failure to write will result in a runtime error.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path of the file to append to.
bytes:
type: '[Byte]'
description: >
The bytes to append to the file.
permissions:
type: 'Int32'
default: 'Int32(0o644)'
description: >
The permissions to set on the file if it is being created.
example: |
(./log.txt).append_bytes([104, 105])!
Path.base_name:
short: base name of a file
description: >
Returns the base name of the file or directory at the specified path.
errors: >
A POSIX filename is an arbitrary sequence of bytes, but `Text` holds
Unicode, so this returns `none` when the name is not valid UTF-8.
return:
type: 'Text?'
description: >
The base name of the file or directory, or `none` if it is not valid
UTF-8.
args:
path:
type: 'Path'
description: >
The path of the file or directory.
example: |
assert (./path/to/file.txt).base_name() == "file.txt"
Path.bytes:
short: get the raw bytes of a path
description: >
Convert a path to a list of the raw bytes that make up its text
representation. This does not read the file at the given path; see
`Path.read_bytes` for that.
return:
type: '[Byte]'
description: >
A list of bytes (`[Byte]`) representing the path's text.
args:
path:
type: 'Path'
description: >
The path.
example: |
assert (/foo/bar).bytes() == [47, 102, 111, 111, 47, 98, 97, 114]
Path.by_line:
short: iterate by line
description: >
Returns an iterator that can be used to iterate over a file one line at a time,
or returns none if the file could not be opened.
return:
type: 'func(->Text?)?'
description: >
An iterator that can be used to get lines from a file one at a time or
none if the file couldn't be read.
args:
path:
type: 'Path'
description: >
The path of the file.
example: |
# Safely handle file not being readable:
if lines := (./file.txt).by_line()
for line in lines
say(line.upper())
else
say("Couldn't read file!")
# Assume the file is readable and error if that's not the case:
for line in (/dev/stdin).by_line()!
say(line.upper())
Path.components:
short: Get the components of a path
description: >
Returns a list of the file components of a path.
errors: >
A POSIX filename is an arbitrary sequence of bytes, but `Text` holds
Unicode, so this returns `none` when the name is not valid UTF-8.
return:
type: '[Text]?'
description: >
Each of the file components of the path in a list, or `none` if any
component is not valid UTF-8. Note: for absolute paths, the first
component will be "/". Trailing slashes are ignored.
args:
path:
type: 'Path'
description: >
The path of the file or directory.
example: |
assert (./foo/baz.txt).components() == [".", "foo", "baz.txt"]
assert (/absolute/path/).components() == ["/", "absolute", "path"]
Path.lines:
short: return the lines in a file
description: >
Returns a list with the lines of text in a file or returns none if the file
could not be opened.
return:
type: '[Text]?'
description: >
A list of the lines in a file or none if the file couldn't be read.
args:
path:
type: 'Path'
description: >
The path of the file.
example: |
lines := (./file.txt).lines()!
Path.can_execute:
short: check execute permissions
description: >
Returns whether or not a file can be executed by the current user/group.
note: >
This answers about the moment it is asked, and another process can change
it immediately after. Do not use it to decide whether an operation will
succeed: attempt the operation and handle its `Failure` instead.
return:
type: 'Bool'
description: >
`yes` if the file or directory exists and the current user has execute permissions, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path of the file to check.
example: |
assert (/bin/sh).can_execute()
assert not (/usr/include/stdlib.h).can_execute()
assert not (/non/existant/file).can_execute()
Path.can_read:
short: check read permissions
description: >
Returns whether or not a file can be read by the current user/group.
note: >
This answers about the moment it is asked, and another process can change
it immediately after. Do not use it to decide whether an operation will
succeed: attempt the operation and handle its `Failure` instead.
return:
type: 'Bool'
description: >
`yes` if the file or directory exists and the current user has read permissions, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path of the file to check.
example: |
assert (/usr/include/stdlib.h).can_read()
assert not (/etc/shadow).can_read()
assert not (/non/existant/file).can_read()
Path.can_write:
short: check write permissions
description: >
Returns whether or not a file can be written by the current user/group.
note: >
This answers about the moment it is asked, and another process can change
it immediately after. Do not use it to decide whether an operation will
succeed: attempt the operation and handle its `Failure` instead.
return:
type: 'Bool'
description: >
`yes` if the file or directory exists and the current user has write permissions, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path of the file to check.
example: |
assert (/tmp).can_write()
assert not (/etc/passwd).can_write()
assert not (/non/existant/file).can_write()
Path.changed:
short: get the last changed time
description: >
Gets the file change time of a file.
note: >
This is the ["ctime"](https://en.wikipedia.org/wiki/Stat_(system_call)#ctime) of a file,
which is _not_ the file creation time.
return:
type: 'Int64?'
description: >
A 64-bit unix epoch timestamp representing when the file or directory was last
changed, or `none` if no such file or directory exists.
args:
path:
type: 'Path'
description: >
The path of the file whose change time you want.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./file.txt).changed() == Int64(1704221100)
assert (./not-a-file).changed() == none
Path.child:
short: append a child to a path
description: >
Return a path that is a child of another path.
return:
type: 'Path'
description: >
A new path representing the child.
args:
path:
type: 'Path'
description: >
The path of a directory.
child:
type: 'Text'
description: >
The name of a child file or directory.
example: |
assert (./directory).child("file.txt") == (./directory/file.txt)
Path.children:
short: get children of a directory
description: >
Returns a list of children (files and directories) within the directory at
the specified path. Optionally includes hidden files. Child ordering is not
specified.
note: >
The paths returned keep the form of the path they came from: the children
of `(./foo)` are relative and the children of `(~/foo)` stay home-based.
Use `.resolved()` on them if you need absolute paths.
errors: >
Returns `none` if the path is not a readable directory.
return:
type: '[Path]?'
description: >
A list of paths for the children, or `none` if the directory could not
be read. An existing but empty directory gives an empty list, not `none`.
args:
path:
type: 'Path'
description: >
The path of the directory.
include_hidden:
type: 'Bool'
default: 'no'
description: >
Whether to include hidden files (those starting with a `.`).
example: |
assert (./directory).children(include_hidden=yes)!.sorted() == [(./directory/.git), (./directory/foo.txt)]
# A directory that can't be read gives `none`, not an empty list:
assert (./not-a-directory).children() == none
Path.concatenated_with:
short: concatenate two paths
description: >
Return the concatenation of two paths. This is what the `++` operator does
on paths. The result is normalized, so `.` and `..` components in the
second path are resolved away.
return:
type: 'Path'
description: >
The second path appended to the first.
args:
a:
type: 'Path'
description: >
The base path.
b:
type: 'Path'
description: >
A relative path to append. It is a runtime error if this is an
absolute or home-based path.
example: |
assert (/foo/bar).concatenated_with((./baz)) == (/foo/bar/baz)
assert ((/foo/bar) ++ (./baz/../qux)) == (/foo/bar/qux)
Path.copy_to:
short: copy a file or directory
description: >
Copies the file or directory from one location to another. This is the same
behavior as `cp -rn -T src dest` or `cp -rf -T src dest` (if `overwrite` is
enabled).
note: >
Copying a tree is many operations rather than one, so unlike `Path.move`
this cannot be indivisible. With `overwrite=no` an existing destination is
never written over, but if one appears while the copy is running it is
skipped silently rather than reported.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path to copy.
dest:
type: 'Path'
description: >
The destination to copy the path to.
overwrite:
type: 'Bool'
default: 'no'
description: >
Whether to permit overwriting the destination if it is an existing file or directory.
example: |
(./file.txt).copy_to(/tmp/copy.txt)!
Path.create_directory:
short: make a directory
description: >
Creates a new directory at the specified path with the given permissions. If
any of the parent directories do not exist, they will be created as needed.
note: >
A directory that already exists is a success, which is what makes this safe
to call when another process may be creating the same directory. Something
that exists but is not a directory is a failure.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path of the directory to create.
permissions:
type: 'Int32'
default: 'Int32(0o755)'
description: >
The permissions to set on the new directory.
recursive:
type: 'Bool'
default: 'yes'
description: >
If set to `yes`, then recursively create any parent directories if they
don't exist, otherwise fail if the parent directory does not exist. When
set to `yes`, this function behaves like `mkdir -p`.
example: |
(./new_directory).create_directory()!
Path.current_dir:
short: get current directory
description: >
Returns the absolute path of the current working directory.
return:
type: 'Path'
description: >
The absolute path of the current directory.
args:
example: |
assert Path.current_dir() == (/home/user/tomo)
Path.each_child:
short: iterate over children of a directory
description: >
Returns an iterator over the children (files and directories) within the
directory at the specified path. Optionally includes hidden files.
Iteration order is not specified.
note: >
The paths returned keep the form of the path they came from: the children
of `(./foo)` are relative and the children of `(~/foo)` stay home-based.
Use `.resolved()` on them if you need absolute paths.
errors: >
Returns `none` if the path is not a readable directory.
return:
type: 'func(->Path?)?'
description: >
An iterator over the children in a directory, or `none` if the directory
could not be read.
args:
path:
type: 'Path'
description: >
The path of the directory.
include_hidden:
type: 'Bool'
default: 'no'
description: >
Whether to include hidden files (those starting with a `.`).
example: |
# Safely handle the directory not being readable:
if children := (/dir).each_child()
for child in children
say("Child: $child")
else
say("Couldn't read the directory!")
# Assume the directory is readable and error if that's not the case:
for child in (/dir).each_child()!
say("Child: $child")
Path.exists:
short: check if a path exists
description: >
Checks if a file or directory exists at the specified path.
note: >
This answers about the moment it is asked. Deciding what to do from it
races with anything else touching the filesystem; prefer operations that
say what they did, such as `Path.create_directory` (which succeeds if the
directory is already there) or `Path.remove` with `ignore_missing`.
return:
type: 'Bool'
description: >
`yes` if the file or directory exists, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path to check.
example: |
assert (/).exists()
Path.expand_home:
short: 'expand ~ to $HOME'
description: >
For home-based paths (those starting with `~`), expand the path to replace the
tilde with and absolute path to the user's `$HOME` directory.
return:
type: 'Path'
description: >
If the path does not start with a `~`, then return it unmodified. Otherwise,
replace the `~` with an absolute path to the user's home directory.
args:
path:
type: 'Path'
description: >
The path to expand.
example: |
# Assume current user is 'user'
assert (~/foo).expand_home() == (/home/user/foo)
# No change
assert (/foo).expand_home() == (/foo)
Path.extension:
short: get file extension
description: >
Returns the file extension of the file at the specified path. Optionally returns the full extension.
note: >
A name with no `.` in it has no extension, and gives `none` rather than an
empty text. A leading `.` does not count, so a dotfile like `(./.git)` has
no extension, and neither does a name ending in a `.`.
errors: >
Also returns `none` when the name is not valid UTF-8, a POSIX filename
being an arbitrary sequence of bytes while `Text` holds Unicode.
return:
type: 'Text?'
description: >
The file extension, not including the leading `.`, or `none` if there is
no extension.
args:
path:
type: 'Path'
description: >
The path of the file.
full:
type: 'Bool'
default: 'yes'
description: >
Whether to return everything after the first `.` in the base name, or
only the last part of the extension.
example: |
assert (./file.tar.gz).extension() == "tar.gz"
assert (./file.tar.gz).extension(full=no) == "gz"
# No "." in the name means no extension:
assert (/foo).extension() == none
assert (./.git).extension() == none
assert (./foo.).extension() == none
Path.files:
short: list files in a directory
description: >
Returns a list of files within the directory at the specified path. Optionally includes hidden files.
note: >
The paths returned keep the form of the path they came from: the children
of `(./foo)` are relative and the children of `(~/foo)` stay home-based.
Use `.resolved()` on them if you need absolute paths.
errors: >
Returns `none` if the path is not a readable directory.
return:
type: '[Path]?'
description: >
A list of file paths, or `none` if the directory could not be read. A
directory with no files in it gives an empty list, not `none`.
args:
path:
type: 'Path'
description: >
The path of the directory.
include_hidden:
type: 'Bool'
default: 'no'
description: >
Whether to include hidden files (those starting with a `.`).
example: |
assert (./directory).files(include_hidden=yes)!.sorted() == [(./directory/file1.txt), (./directory/file2.txt)]
Path.glob:
short: find files in a directory matching a glob
description: >
Search a directory for paths matching a glob pattern. The pattern is
relative to the directory, and unlike `Path.matches_glob` it is anchored:
it has to account for every component below the directory.
note: >
Details of the pattern syntax:
- `?`, `*`, and character classes like `[ch]` match within a single
component and never across a `/`.
- Files or directories beginning with `.` will not match `*`, but will
match `.*`. The paths "." and ".." are never included in the results.
- `**` stands for zero or more components, so `"**/*.txt"` finds `.txt`
files at any depth. Unlike the other syntax, this cannot be answered by
`glob(3)`, so such a pattern is served by walking the directory tree.
`**` spans hidden components too, which is what makes it agree with
`Path.matches_glob`. It does *not* descend through a symbolic link to a
directory, since a link pointing back at an ancestor would never
terminate; an explicit `*` component still matches one, so
`"*/x.txt"` can find a file that `"**/x.txt"` will not.
- The `{a,b}` alternation syntax is not supported.
Results keep the form of the directory they came from, so globbing
`(./src)` yields relative paths and globbing `(~/src)` yields home-based
ones. Every path returned satisfies `Path.matches_glob` for the same
pattern.
errors: >
Returns `none` if the directory could not be read. A pattern that simply
matches nothing gives an empty list, not `none`.
return:
type: '[Path]?'
description: >
A sorted list of the paths that match, or `none` if the directory could
not be read.
args:
path:
type: 'Path'
description: >
The directory to search.
pattern:
type: 'Text'
description: >
The glob pattern to match against, relative to the directory. An empty
pattern matches nothing.
example: |
# A directory containing: foo.txt, baz.txt, qux.jpg, .hidden, sub/deep.txt
assert (./dir).glob("*.txt")! == [(./dir/baz.txt), (./dir/foo.txt)]
assert (./dir).glob(".*")! == [(./dir/.hidden)]
assert (./dir).glob("sub/*.txt")! == [(./dir/sub/deep.txt)]
# "**" is zero or more components:
assert (./dir).glob("**/*.txt")! == [(./dir/baz.txt), (./dir/foo.txt), (./dir/sub/deep.txt)]
# A pattern matching nothing is an empty list:
assert (./dir).glob("*.xxx")! == []
# A directory that can't be read is none:
assert (./not-a-directory).glob("*") == none
Path.group:
short: get the owning group
description: >
Get the owning group of a file or directory.
return:
type: 'Text?'
description: >
The name of the group which owns the file or directory, or `none` if the path does not exist.
args:
path:
type: 'Path'
description: >
The path whose owning group to get.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (/bin).group() == "root"
assert (/non/existent/file).group() == none
Path.has_extension:
short: check if a path has a given extension
description: >
Return whether or not a path has a given file extension. Unlike
`Path.extension`, this reads the raw bytes of the name and so works on
names that are not valid UTF-8.
return:
type: 'Bool'
description: >
Whether or not the path has the given extension.
args:
path:
type: 'Path'
description: >
A path.
extension:
type: 'Text'
description: >
A file extension (leading `.` is optional). If empty, the check will
test if the file does not have any file extension.
example: |
assert (/foo.txt).has_extension("txt")
assert (/foo.txt).has_extension(".txt")
assert (/foo.tar.gz).has_extension("gz")
assert not (/foo.tar.gz).has_extension("zip")
# Asking for an empty extension is the same question as `.extension()`
# being `none`, for any name that is valid UTF-8:
assert (/foo).has_extension("")
assert (/).has_extension("")
Path.is_directory:
short: check if a path is a directory
description: >
Checks if the path represents a directory. Optionally follows symbolic links.
return:
type: 'Bool'
description: >
`yes` if the path is a directory, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path to check.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./directory/).is_directory()
assert not (./file.txt).is_directory()
Path.is_file:
short: check if a path is a file
description: >
Checks if the path represents a file. Optionally follows symbolic links.
return:
type: 'Bool'
description: >
`yes` if the path is a file, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path to check.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./file.txt).is_file()
assert not (./directory/).is_file()
Path.is_pipe:
short: check if a path is a named pipe
description: >
Checks if the path represents a named pipe (a FIFO). Optionally follows
symbolic links.
return:
type: 'Bool'
description: >
`yes` if the path is a named pipe, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path to check.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./my-fifo).is_pipe()
assert not (./file.txt).is_pipe()
Path.is_socket:
short: check if a path is a socket
description: >
Checks if the path represents a socket. Optionally follows symbolic links.
return:
type: 'Bool'
description: >
`yes` if the path is a socket, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path to check.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./socket).is_socket()
Path.is_symlink:
short: check if a path is a symbolic link
description: >
Checks if the path represents a symbolic link.
return:
type: 'Bool'
description: >
`yes` if the path is a symbolic link, otherwise `no`.
args:
path:
type: 'Path'
description: >
The path to check.
example: |
assert (./link).is_symlink()
Path.matches_glob:
short: check if a path matches a glob
description: >
Return whether or not a path matches a given glob. This is a pattern match,
not a filesystem operation, and it reads the raw bytes of the path, so it
works on names that are not valid UTF-8.
note: >
The pattern is split on `/` and matched against the path's components from
the back, so a pattern matches any trailing run of components:
- `"*.txt"` asks about the file's name wherever it sits, so it matches
`(./foo.txt)`, `(/tmp/dir/foo.txt)`, and `(~/foo.txt)` alike. The answer
does not depend on which form the path was written in.
- `"dir/*.txt"` matches a `.txt` file directly inside any `dir`.
- A pattern beginning with `/` can only line up at the front of the path,
so `"/tmp/*.txt"` pins the whole path rather than matching a suffix. A
pattern beginning with `./` likewise only matches `./`-form paths.
- `**` stands for zero or more components, so `"**/*.txt"` matches a `.txt`
file at any depth, including none.
- `?`, `*`, and character classes like `[ch]` match within a single
component and never across a `/`. A `.` at the start of a component must
be matched by a literal `.`, so `*` does not match hidden files.
- The `{a,b}` alternation syntax is not supported.
errors: >
An empty pattern matches nothing.
return:
type: 'Bool'
description: >
Whether or not the path matches the given glob.
args:
path:
type: 'Path'
description: >
The path to check.
glob:
type: 'Text'
description: >
The glob pattern to check.
example: |
# A bare pattern asks about the file's name, whatever form the path is in:
assert (./file.txt).matches_glob("*.txt")
assert (/tmp/dir/file.txt).matches_glob("*.txt")
assert not (./file.txt).matches_glob("*.jpg")
# More components match a longer suffix:
assert (./src/file.c).matches_glob("src/*.[ch]")
assert (./src/file.c).matches_glob("./src/*.[ch]")
# A leading "/" anchors the pattern to the whole path:
assert (/tmp/dir/file.txt).matches_glob("/tmp/dir/*.txt")
assert not (/other/dir/file.txt).matches_glob("/tmp/dir/*.txt")
# "**" is zero or more components:
assert (./a/b/c/file.txt).matches_glob("**/*.txt")
assert (./file.txt).matches_glob("**/*.txt")
# "*" does not match a leading "." or cross a "/":
assert not (./dir/.hidden).matches_glob("*")
assert (./dir/.hidden).matches_glob(".*")
Path.modified:
short: get file modification time
description: >
Gets the file modification time of a file.
return:
type: 'Int64?'
description: >
A 64-bit unix epoch timestamp representing when the file or directory was last
modified, or `none` if no such file or directory exists.
args:
path:
type: 'Path'
description: >
The path of the file whose modification time you want.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (./file.txt).modified() == Int64(1704221100)
assert (./not-a-file).modified() == none
Path.move:
short: move a file or directory
description: >
Moves the file or directory from one location to another.
note: >
With `overwrite=no`, the refusal and the move are a single operation the
kernel performs indivisibly, so another process cannot slip a file into the
destination in between. Moving a directory across filesystems is the one
case that cannot be done that way and falls back to checking first.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path to move.
dest:
type: 'Path'
description: >
The destination to move the path to.
overwrite:
type: 'Bool'
default: 'no'
description: >
Whether to permit overwriting the destination if it is an existing file or directory.
example: |
(./file.txt).move(/tmp/renamed.txt)!
Path.owner:
short: get file owner
description: >
Get the owning user of a file or directory.
return:
type: 'Text?'
description: >
The name of the user who owns the file or directory, or `none` if the path does not exist.
args:
path:
type: 'Path'
description: >
The path whose owner to get.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
assert (/bin).owner() == "root"
assert (/non/existent/file).owner() == none
Path.parent:
short: get parent directory
description: >
Returns the parent directory of the file or directory at the specified path.
return:
type: 'Path?'
description: >
The path of the parent directory or `none` if the path is `(/)` (the file root).
args:
path:
type: 'Path'
description: >
The path of the file or directory.
example: |
assert (./path/to/file.txt).parent() == (./path/to)
Path.read:
short: read file contents
description: >
Reads the contents of the file at the specified path or none if the file
could not be read.
return:
type: 'Text?'
description: >
The contents of the file. If the file could not be read, none will be
returned. If the file can be read, but is not valid UTF8 data, an error
will be raised.
args:
path:
type: 'Path'
description: >
The path of the file to read.
example: |
assert (./hello.txt).read() == "Hello"
assert (./nosuchfile.xxx).read() == none
Path.read_bytes:
short: read file contents as bytes
description: >
Reads the contents of the file at the specified path or none if the file
could not be read.
return:
type: '[Byte]?'
description: >
The byte contents of the file. If the file cannot be read, none will be
returned.
args:
path:
type: 'Path'
description: >
The path of the file to read.
limit:
type: 'Int?'
default: 'none'
description: >
A limit to how many bytes should be read.
example: |
assert (./hello.txt).read_bytes()! == [72, 101, 108, 108, 111]
assert (./nosuchfile.xxx).read_bytes() == none
Path.relative_to:
short: apply a relative path to another
description: >
Returns the path relative to a given base path.
note: >
The result has no leading `./`, so a path naming something inside the base
path cannot be written as a path literal. Results that climb out of the
base path begin with `../` and can.
return:
type: 'Path'
description: >
A relative path from the reference point to the given path.
args:
path:
type: 'Path'
description: >
The path to convert.
relative_to:
type: 'Path'
description: >
The base path for the relative path. Unlike `Path.resolved`, this
argument is required.
example: |
# A path relative to itself is the current directory:
assert (/tmp).relative_to((/tmp)) == (.)
assert "$((./path/to/file.txt).relative_to((./path)))" == "to/file.txt"
assert "$((/tmp/foo).relative_to((/tmp)))" == "foo"
assert (/a/b/c).relative_to((/a/x)) == (../b/c)
Path.remove:
short: remove a file or directory
description: >
Removes the file or directory at the specified path. A runtime error is raised if something goes wrong.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path to remove.
ignore_missing:
type: 'Bool'
default: 'no'
description: >
Whether to ignore errors if the file or directory does not exist.
example: |
(./file.txt).remove()!
Path.resolved:
short: resolve a path
description: >
Resolves the absolute path of the given path relative to a base path. By
default, the base path is the current directory. A relative base is itself
resolved against the current directory first, so the result is always
absolute.
return:
type: 'Path'
description: >
The resolved absolute path.
args:
path:
type: 'Path'
description: >
The path to resolve.
relative_to:
type: 'Path'
default: '(./)'
description: >
The base path for resolution.
example: |
# Assume the current directory is /home/user
assert (~/foo).resolved() == (/home/user/foo)
assert (./foo).resolved() == (/home/user/foo)
assert (./path/to/file.txt).resolved(relative_to=(/foo)) == (/foo/path/to/file.txt)
Path.set_owner:
short: set the owner
description: >
Set the owning user and/or group for a path.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path to change the permissions for.
owner:
type: 'Text?'
default: 'none'
description: >
If non-none, the new user to assign to be the owner of the file.
group:
type: 'Text?'
default: 'none'
description: >
If non-none, the new group to assign to be the owner of the file.
follow_symlinks:
type: 'Bool'
default: 'yes'
description: >
Whether to follow symbolic links.
example: |
(./file.txt).set_owner(owner="root", group="wheel")!
Path.sibling:
short: get another path in the same directory
description: >
Return a path that is a sibling of another path (i.e. has the same parent,
but a different name). This is equivalent to `.parent().child(name)`
errors: >
Returns `none` for a path with no parent to put a sibling beside, which is
the file root `(/)`.
return:
type: 'Path?'
description: >
A new path representing the sibling, or `none` if the path is `(/)`.
args:
path:
type: 'Path'
description: >
A path.
name:
type: 'Text'
description: >
The name of a sibling file or directory.
example: |
assert (/foo/baz).sibling("doop") == (/foo/doop)
assert (/).sibling("doop") == none
Path.subdirectories:
short: get subdirectories
description: >
Returns a list of subdirectories within the directory at the specified path. Optionally includes hidden subdirectories.
note: >
The paths returned keep the form of the path they came from: the children
of `(./foo)` are relative and the children of `(~/foo)` stay home-based.
Use `.resolved()` on them if you need absolute paths.
errors: >
Returns `none` if the path is not a readable directory.
return:
type: '[Path]?'
description: >
A list of subdirectory paths, or `none` if the directory could not be
read. A directory with no subdirectories gives an empty list, not `none`.
args:
path:
type: 'Path'
description: >
The path of the directory.
include_hidden:
type: 'Bool'
default: 'no'
description: >
Whether to include hidden subdirectories (those starting with a `.`)
example: |
assert (./directory).subdirectories()!.sorted() == [(./directory/subdir1), (./directory/subdir2)]
assert (./directory).subdirectories(include_hidden=yes)!.sorted() == [(./directory/.git), (./directory/subdir1), (./directory/subdir2)]
Path.unique_directory:
short: create a directory with a unique name
description: >
Generates a unique directory path based on the given path. Useful for creating temporary directories.
errors: >
Returns `none` if the directory could not be created.
return:
type: 'Path?'
description: >
A unique directory path after creating the directory, or `none` if it
could not be created.
args:
path:
type: 'Path'
description: >
The base path for generating the unique directory. The last six letters of this path must be `XXXXXX`.
example: |
created := (/tmp/my-dir.XXXXXX).unique_directory()!
assert created.is_directory()
created.remove()!
Path.walk:
short: walk a filetree
description: >
Returns an iterator that efficiently recursively walks over every file and
subdirectory in a given directory. The iteration order is not defined, but
in practice it may look a lot like a breadth-first traversal.
note: >
The path itself is always included in the iteration. The paths returned keep
the form of the path they came from: walking `(./foo)` yields relative paths
and walking `(~/foo)` yields home-based ones.
errors: >
Returns `none` if there is nothing at the given path. A path that exists but
is not a directory walks over just itself.
return:
type: 'func(->Path?)?'
description: >
An iterator that recursively walks over every file and subdirectory, or
`none` if the path does not exist.
args:
path:
type: 'Path'
description: >
The path to begin the walk.
include_hidden:
type: 'Bool'
default: 'no'
description: >
Whether to include hidden files (those starting with a `.`)
follow_symlinks:
type: 'Bool'
default: 'no'
description: >
Whether to follow symbolic links. Caution: if set to 'yes', it is
possible for this iterator to get stuck in a loop, using increasingly
large amounts of memory.
example: |
for p in (/tmp).walk()!
say("File or dir: $p")
# The path itself is always included:
assert [p for p in (./file.txt).walk()!] == [(./file.txt)]
# A path that isn't there has nothing to walk:
assert (./not-a-path).walk() == none
Path.with_extension:
short: replace or add a file extension
description: >
Return a path with the given file extension, either replacing the path's
existing extension or adding to it.
return:
type: 'Path'
description: >
A new path with the requested extension.
args:
path:
type: 'Path'
description: >
A path.
extension:
type: 'Text'
description: >
The file extension to apply (a leading `.` is optional). If empty, the
extension is removed entirely.
replace:
type: 'Bool'
default: 'yes'
description: >
If `yes`, the path's existing extension is replaced. If `no`, the new
extension is appended to the existing one.
example: |
assert (./file.tar.gz).with_extension("zip") == (./file.zip)
assert (./file.tar.gz).with_extension(".zip") == (./file.zip)
assert (./file.tar.gz).with_extension("") == (./file)
assert (./file.tar.gz).with_extension("zip", replace=no) == (./file.tar.gz.zip)
assert (./file).with_extension("txt") == (./file.txt)
Path.write:
short: write to a file
description: >
Writes the given text to the file at the specified path, creating the file if
it doesn't already exist. Sets the file permissions as specified. If the file
writing cannot be successfully completed, a runtime error is raised.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path of the file to write to.
text:
type: 'Text'
description: >
The text to write to the file.
permissions:
type: 'Int32'
default: 'Int32(0o644)'
description: >
The permissions to set on the file if it is created.
example: |
(./file.txt).write("Hello, world!")!
Path.writer:
short: create a file writer
description: >
Returns a function that can be used to repeatedly write to the same file.
note: >
The file writer will keep its file descriptor open after each write (unless
the `close` argument is set to `yes`). If the file writer is never closed,
it will be automatically closed when the file writer is garbage collected.
return:
type: 'func(text:Text, close:Bool=no -> Result)'
description: >
Returns a function that can repeatedly write to the same file. If `close`
is set to `yes`, then the file will be closed after writing. If this
function is called again after closing, the file will be reopened for
appending.
args:
path:
type: 'Path'
description: >
The path of the file to write to.
append:
type: 'Bool'
default: 'no'
description: >
If set to `yes`, writes to the file will append. If set to `no`, then
the first write to the file will overwrite its contents and subsequent
calls will append.
permissions:
type: 'Int32'
default: 'Int32(0o644)'
description: >
The permissions to set on the file if it is created.
example: |
write := (./file.txt).writer()
write("Hello\n")!
write("world\n", close=yes)!
Path.byte_writer:
short: create a byte-based file writer
description: >
Returns a function that can be used to repeatedly write bytes to the same
file.
note: >
The file writer will keep its file descriptor open after each write (unless
the `close` argument is set to `yes`). If the file writer is never closed,
it will be automatically closed when the file writer is garbage collected.
return:
type: 'func(bytes:[Byte], close:Bool=no -> Result)'
description: >
Returns a function that can repeatedly write bytes to the same file. If
`close` is set to `yes`, then the file will be closed after writing. If
this function is called again after closing, the file will be reopened
for appending.
args:
path:
type: 'Path'
description: >
The path of the file to write to.
append:
type: 'Bool'
default: 'no'
description: >
If set to `yes`, writes to the file will append. If set to `no`, then
the first write to the file will overwrite its contents and subsequent
calls will append.
permissions:
type: 'Int32'
default: 'Int32(0o644)'
description: >
The permissions to set on the file if it is created.
example: |
write := (./file.txt).byte_writer()
write("Hello\n".utf8())!
write("world\n".utf8(), close=yes)!
Path.write_bytes:
short: write bytes to a file
description: >
Writes the given bytes to the file at the specified path, creating the file if
it doesn't already exist. Sets the file permissions as specified. If the file
writing cannot be successfully completed, a runtime error is raised.
return:
type: 'Result'
description: >
Either `Success` or `Failure{reason}`.
args:
path:
type: 'Path'
description: >
The path of the file to write to.
bytes:
type: '[Byte]'
description: >
A list of bytes to write to the file.
permissions:
type: 'Int32'
default: 'Int32(0o644)'
description: >
The permissions to set on the file if it is created.
example: |
(./file.txt).write_bytes([104, 105])!
Path.write_unique:
short: write to a uniquely named file
description: >
Writes the given text to a unique file path based on the specified path. The
file is created if it doesn't exist. This is useful for creating temporary
files.
return:
type: 'Path?'
description: >
The path of the newly created unique file, or `none` if the file could
not be created.
args:
path:
type: 'Path'
description: >
The base path for generating the unique file. This path must include
the string `XXXXXX` in the file base name.
text:
type: 'Text'
description: >
The text to write to the file.
example: |
# The created path has the XXXXXX replaced by random characters,
# e.g. (./file-27QHtq.txt):
created := (./file-XXXXXX.txt).write_unique("Hello, world!")!
assert created.read()! == "Hello, world!"
created.remove()!
Path.write_unique_bytes:
short: write bytes to a uniquely named file
description: >
Writes the given bytes to a unique file path based on the specified path. The
file is created if it doesn't exist. This is useful for creating temporary
files.
return:
type: 'Path?'
description: >
The path of the newly created unique file, or `none` if the file could
not be created.
args:
path:
type: 'Path'
description: >
The base path for generating the unique file. This path must include
the string `XXXXXX` in the file base name.
bytes:
type: '[Byte]'
description: >
The bytes to write to the file.
example: |
# The created path has the XXXXXX replaced by random characters,
# e.g. (./file-27QHtq.txt):
created := (./file-XXXXXX.txt).write_unique_bytes([1, 2, 3])!
assert created.read_bytes()! == [1, 2, 3]
created.remove()!
|