Zulip Chat Archive

Stream: Is there code for X?

Topic: create a file with given path if not exists


Asei Inoue (Dec 30 2023 at 12:02):

Given a specific path fp : FilePath, what code should I write to create a file with fp as its path if it does not already exist?

Asei Inoue (Dec 30 2023 at 12:08):

I want a writeNew version of IO.FS.writeFile.

Asei Inoue (Dec 30 2023 at 12:18):

This code raise an error "permission denied"

def createFile (path : FilePath) (content : String) : IO Unit := do
  let h  IO.FS.Handle.mk path IO.FS.Mode.write
  h.putStr content

Asei Inoue (Dec 30 2023 at 12:37):

I managed to reach this code:

def createFile (path : FilePath) (content : String) : IO Unit := do
  match path.parent with
  | none => IO.FS.writeFile path content
  | some parent =>
    IO.FS.createDirAll parent
    IO.FS.writeFile path content

Asei Inoue (Dec 30 2023 at 12:37):

But I think there should be a more consise way...!


Last updated: May 02 2025 at 03:31 UTC