Reference · Files and processes
delete_file
signature
delete_file(str) -> null | error(str)What it does
Removes a file under the program's folder, or a folder and everything in it. It gives
null when the path is gone, also when there was nothing there, and an error value
when the path leaves the folder or names the folder itself.
Examples
write_file("tmp/a.txt", "a")print(delete_file("tmp/a.txt"))print(read_file("tmp/a.txt") ?? "gone")output
nullgoneEdge cases
- Deleting what is not there is
null, not an error: a second delete is harmless. - A folder is removed with everything in it.
".","",".."and a path starting at/are error values: the program's own folder is never removed, and nothing outside it is touched.- The program's own files are refused as
write_filerefuses them (its.qwefiles,.env, the databases it has open, in any case and as Windows reads a name), and so is a folder that holds one:delete_file("data")withdata/app.dbopen is an error value, and nothing is deleted. - Links are followed, and where they lead is what counts: a link inside the folder
that leads inside it works as its target would, and one that leads out of the
folder, or leads nowhere, is an error value, and nothing is deleted: a path is judged where it leads, so
delete_file("data")on a link out of the folder is refused too. The first such path is said once in the log, with where it leads and--allow-path FOLDER, the option ofquillo runthat lets the program reach a folder outside on purpose (quillo run main.qwe --allow-path /srv/media; see the options).
print(delete_file("nope.txt"), delete_file("nope.txt"))write_file("tmp/a/b.txt", "b")write_file("tmp/c.txt", "c")print(delete_file("tmp"), read_file("tmp/c.txt") ?? "gone")print(delete_file("."), delete_file(""))print(delete_file("../x"), delete_file("/tmp/x"))output
null nullnull goneerr("delete_file: path must be inside the program's folder: .") err("delete_file: path must be inside the program's folder: ")err("delete_file: path must be inside the program's folder: ../x") err("delete_file: path must be inside the program's folder: /tmp/x")Errors
A path outside the folder, or one of the program's own files or a folder holding one,
is an error value, which ? passes on; a path that is not a string is a runtime error.
let r = delete_file("..")print(is_err(r), r.error)output
true delete_file: path must be inside the program's folder: ..db.open("app.db")print(delete_file("app.db"), delete_file("MAIN.qwe"))output
err("delete_file: will not delete app.db: it is the database the program has open") err("delete_file: will not delete MAIN.qwe: it is one of the program's own files")this one fails
print(delete_file(1))output
line 1: `delete_file` expects a str as argument 1, got intChaining
write_file("a.txt", "a")write_file("b.txt", "b")print(["a.txt", "b.txt"] | map(delete_file))print("a.txt" | read_file ?? "gone")output
[null, null]goneSee also
write_file, read_file, save_upload
Thread
Thread 0 posts
On delete_file(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo