JanetDocsSourcePlaygroundI'm feeling luckyCommunityGitHub sign in

slurp

core-api


    function
    boot.janet on line 1870, column 1

    (slurp path)

    Read all data from a file with name `path` and then close the file.


See also:spit2 examplesSign in to add an example
Loading...
# In Linux, "everything is a file" so:

(def pid (os/getpid)) 

pid
# =>
367537

(def statm-path (string "/proc/" pid "/statm"))

statm-path
# =>
"/proc/367537/statm"

(slurp statm-path)
# =>
@"1380 971 683 82 0 362 0\n"
sogaiuPlayground
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt")  # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt"))  # => @["hello" "world" ""]

(defn slurp-lines [path]
  (string/split "\n" (slurp path)))

(slurp-lines "foo.txt")  # => @["hello" "world" ""]

(string/join @["hello" "world" ""] "\n")  # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" ""] "\n"))
# The contents of foo.txt and foo2.txt are now identical.

(defn spit-lines [path lines]
  (spit path (string/join lines "\n")))

(spit-lines "foo3.txt" (slurp-lines "foo.txt"))
# The contents of foo.txt and foo3.txt are now identical.
cellularmitosisPlayground