Muhammad Lukman Low
Muhammad Lukman Low

Reputation: 8533

Is there something equivalent to enhpath in Elixir?

In Python the third party enhpath.py library allows for something like this:

In [2]: path("/home/lowks/Documents").listdir()
Out[2]: 
[path('/home/lowks/Documents/5fc72638da7598b350733c5a51fce596.jpeg'),
 path('/home/lowks/Documents/Prop-API-01.pdf'),]

The File module in Elixir does something like this:

iex(1)> File.ls!("/home/lowks/Documents")
["5fc72638da7598b350733c5a51fce596.jpeg","Prop-API-01.pdf"]

This does not work so well for me as I want the absolute path like the one above, so I do something like:

iex(2)> File.ls!("/home/lowks/Documents") |> Enum.map(&Path.absname(&1))
["/home/lowks/5fc72638da7598b350733c5a51fce596.jpeg",
 "/home/lowks/dsr_excel_csv.sql"]

but from the output it can be seen that the absolute path is joined to the cwd working directory rather than the correct one "/home/lowks/Documents". This looks and behaves like the stock python library for processing paths in Python, my question is do they have something that behaves like third party Python path libraries ?

Upvotes: 0

Views: 178

Answers (3)

Alexei Sholik
Alexei Sholik

Reputation: 7469

You could also do it like this:

def dir_files(dir_path) do
  File.ls!(dir_path) |> Enum.map(&Path.join(dir_path, &1))
end

Upvotes: 0

Muhammad Lukman Low
Muhammad Lukman Low

Reputation: 8533

I have searched and have not found any libs for this, so I took it upon myself to create one:

https://github.com/lowks/Radpath

Upvotes: 1

sasajuric
sasajuric

Reputation: 6059

It seems to me, your second version would work if you used Path.expand/2 instead of Path.absname/1:

iex(1)> path = ...
iex(2)> File.ls!(path) |> Enum.map(&Path.expand(&1, path))

Upvotes: 2

Related Questions