tomo-koans/lesson-templates/lesson-13-paths.tm

45 lines
719 B
Plaintext
Raw Normal View History

2025-03-24 19:16:58 -07:00
# Paths
func main():
# Tomo includes a built-in literal type for file paths
# A path is inside parentheses and begins with `/`, `~`, `.` or `..`
2025-03-24 23:25:54 -07:00
path := (/tmp/test-file.txt)
>> path
2025-03-24 19:16:58 -07:00
= /tmp/test-file.txt
2025-03-24 23:25:54 -07:00
path:write("first line")
>> path:read()
2025-03-24 19:16:58 -07:00
= "???"
2025-03-24 23:25:54 -07:00
path:append("
2025-03-24 19:16:58 -07:00
second line
")
2025-03-24 23:25:54 -07:00
>> path:exists()
2025-03-24 19:16:58 -07:00
= yes
# You can iterate over a file by lines:
2025-03-24 23:25:54 -07:00
>> upper_lines := [line:upper() for line in path:by_line()!]
2025-03-24 19:16:58 -07:00
= [???]
2025-03-24 23:25:54 -07:00
>> path:parent()
2025-03-24 19:16:58 -07:00
= /???
2025-03-24 23:25:54 -07:00
>> path:extension()
2025-03-24 19:16:58 -07:00
= "???"
2025-03-24 23:25:54 -07:00
>> path:parent():child("other-file.txt")
2025-03-24 19:16:58 -07:00
= /???
>> dir := (/tmp/test-*.txt):glob()
= [???]
2025-03-24 23:25:54 -07:00
path:remove()
2025-03-24 19:16:58 -07:00
2025-03-24 23:25:54 -07:00
>> path:exists()
2025-03-24 19:16:58 -07:00
= ???