Asking help why File.write is throwing {:error, :enoent} when writing binary

Hello, i was following a simple guide about saving encoded qr code from an Elixir library eqrcode… The given example code was simple…

qr_code_png =
  qr_code_content
  |> EQRCode.encode()
  |> EQRCode.png()

File.write("path/where/you/want/to/save.png", qr_code_png, [:binary])

But when i tried it, passing the path /images/save.png (because i want to save it in static/images folder but it’s not working. When i check the logs, i found out File.write is returning these {:error, :enoent} tuple.

Any idea how can i possibly fix it?

Hello there!

Make sure directory to which you are trying to save file exists.

iex> File.write("/non-existent/path/file.txt", "data")
{:error, :enoent}
iex> File.write("/tmp/file.txt", "data")              
:ok
iex> 
1 Like

Thanks for this idea sir. One more question, how can i possibly get the parh of images in assets/static/images? I want to write this file in that folder :pray:

Why would You want to put in assets/static/images?

Probably public/static/images is a better place…

You might use :code.priv_dir(:yourapp) as a reference to the public folder.

2 Likes

Okay i’ll try. Thank you sir.

The mistake you are making here is confusing the url path with the file system path. File.write needs a file system path.

1 Like