File.write - problem with path

If i write in IEX:

File.write!("files/myfile", Base.decode64!("somebase64"))

It works, but if i put this code to my app it doesn’t. Why? I know something is wrong with path but don’t know what, i tried set this path manually, but nothing work.

You need to define what you mean with “it doesn’t work”. Does it write the file into the wrong place? Or does it crash?

Note that the working directory may be different when running with mix and again when running a Distillery release. So it’s suggested you do not use hardcoded relative paths as they may point to the wrong place. To get a path to a dir inside your app, you can use Application.app_dir(:myapp, "dir"). See this example from my code that gets a path to the priv dir: https://gitlab.com/code-stats/code-stats/blob/master/lib/code_stats_web/geolix.ex#L8

  @spec init() :: :ok
  def init() do
    db_dir = Application.app_dir(:code_stats, "priv")

    databases = [
      %{
        id: :city,
        adapter: Geolix.Adapter.MMDB2,
        source: Path.join([db_dir, "geoip-cities.gz"])
      },
      %{
        id: :country,
        adapter: Geolix.Adapter.MMDB2,
        source: Path.join([db_dir, "geoip-countries.gz"])
      }
    ]

    Application.put_env(:geolix, :databases, databases)
  end
2 Likes

Edit: thanks, everything is work;)

1 Like