Working with file paths: joining them, separating the name and the extension, absolute and relative paths, and a safety check.
import fs.path
The fs.path package takes paths apart and puts them together: join, split, clean, make absolute or relative, and check that a path from a user stays inside a folder. Every function is a thin name over the same function in filepath (with Filename for Base, Dirname and Parent for Dir), so pick one of the two packages and use it throughout a program. One name differs in meaning: filepath.Base keeps the extension, while path.Basename drops it; fs.path reads naturally next to fs and fs.dir.
Do not build paths by concatenating strings; Join puts the separator in for you. The separator is always /, on Windows too: every function first turns \ into /, and Windows accepts / in paths. When a path comes from a user, check it with IsSafe before opening it, so that nobody climbs out of the permitted directory with ../...
Every function here works on the text of the path alone. None of them looks at the disk, except Abs, Rel and IsSafe with a relative path, which read the current directory. To create, read or list files, combine this package with fs.dir, fs or io, as the guide below does.
What people get wrong:
•
Expecting Join to clean up. It only adds or merges the separator, so Join("a/b", "../c") is a/b/../c; pass the result through Clean. An absolute second part does not replace the first either, unlike Python's os.path.join.
•
Reading Ext as the whole extension. It is only the last one: .gz for backup.tar.gz, and empty for a dot file such as .bashrc.
•
Comparing paths as text without Clean. docs/./cv.pdf and docs/cv.pdf are the same file. Comparisons are also case sensitive, which is wrong on Windows and on default macOS disks.
Serving a file a user asked for
A download handler gets a name such as 2026/report.pdf from the request and has to turn it into a file inside one folder. IsSafe simplifies the joined path first and then checks that it still starts with the folder, so a/../b.txt is fine while ../secret.txt and a/../../etc/passwd are refused. An empty name is refused too.
fs-path-guide-safe.salam
import fs.path
funcresolve(root: str, requested: str): str:
ifnot path.IsSafe(root, requested): ret""endret path.Clean(path.Join(root, requested))
endfuncmain:
root := "/srv/uploads"each name in [
"avatar.png", "2026/report.pdf", "a/../b.txt", "../secret.txt", "a/../../etc/passwd",
"/etc/passwd", ""
]:
full := resolve(root, name)
if full.Len() == 0:
println"refused", "[" + name + "]"else:
println"serve ", full
endendend
Look at the /etc/passwd line: Join does not let an absolute second part replace the first, so the result lands inside the folder and is safe. The check works on text only; it does not follow symbolic links, so do not put links that point outside the folder in a directory you serve.
Mapping one folder tree onto another
A static site generator reads content/blog/hello.md and writes public/blog/hello.html. Rel gives the part below the source folder, Dirname and Stem take it apart, and Join and Clean build the new path. Stem removes only the last extension, so salam.v2.md keeps its .v2.
For a file directly in the source folder Dirname returns .; the final Clean removes the /. that Join leaves behind. The example uses absolute paths because Rel makes relative ones absolute against the current directory first.
Making a new name next to a file
To put a backup, a thumbnail or a converted copy next to the original, keep the folder and change only the name. Here Stem and Ext turn plan.md into plan.bak.md, and fs.dir and io do the actual work on disk.
fs-path-guide-backup.salam
import fs.path
import fs.dir
import io
funcbackup_name(p: str): str:
name := path.Stem(p).Concat(".bak").Concat(path.Ext(p))
ret path.Join(path.Dirname(p), name)
endfuncmain:
dir.MkdirAll("notes/2026")
each p in ["notes/todo.txt", "notes/2026/plan.md"]:
io.WriteFile(p, "draft of ".Concat(path.Filename(p)))
copy := backup_name(p)
io.WriteFile(copy, io.ReadFile(p))
println p, "saved as", copy
endprintln io.ReadFile("notes/2026/plan.bak.md")
end
Output
notes/todo.txt saved as notes/todo.bak.txt
notes/2026/plan.md saved as notes/2026/plan.bak.md
draft of plan.md
Which function for which job
Several functions here have close names. Filename keeps the extension; Basename and Stem both drop it. Normalize only changes \ to /; Clean is the one that resolves . and ...
You want
Call
For /home/sara/docs/cv.pdf
The folder
Dirname or Parent
/home/sara/docs
The name with its extension
Filename
cv.pdf
The name without its extension
Stem or Basename
cv
The extension
Ext
.pdf
Folder and name at once
Split
dir and base fields
Remove ., .. and double slashes
Clean
unchanged
The path from one folder to another
Rel
docs/cv.pdf from /home/sara
Check a path from a user
IsSafe
true inside the base folder
Validate only rejects an empty string. It is not a security check; use IsSafe for that.
Simplifies a path: it resolves . and .., removes repeated and trailing separators, and gives . for an empty path. A .. above the root of an absolute path is dropped.
The example for this item is shown with the ones above: fs-path-Join.salam
True when p, joined to base and simplified, is base itself or lies inside it. An empty p is refused. It works on text and does not follow symbolic links.
The example for this item is shown with the ones above: fs-path-Join.salam
Reports whether name matches the glob pattern; the same as filepath.Match: * and ? stay inside one path part, [a-c] and [!a-c] are sets, \ quotes, ** crosses /.