0

在 Python 中,第三方 enhpath.py 库允许这样的事情:

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

Elixir 中的 File 模块执行以下操作:

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

这对我来说效果不佳,因为我想要像上面那样的绝对路径,所以我做了类似的事情:

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

但从输出中可以看出,绝对路径加入了 cwd 工作目录,而不是正确的“/home/lowks/Documents”。这看起来和行为就像用于处理 Python 中路径的股票 python 库,我的问题是它们是否有一些行为类似于第三方 Python 路径库的东西?

4

3 回答 3

2

在我看来,如果您使用Path.expand/2而不是Path.absname/1

iex(1)> path = ...
iex(2)> File.ls!(path) |> Enum.map(&Path.expand(&1, path))
于 2014-03-11T16:21:52.083 回答
1

我已经搜索并没有找到任何库,所以我自己创建了一个:

https://github.com/lowks/Radpath

于 2014-03-14T01:01:26.920 回答
0

你也可以这样做:

def dir_files(dir_path) do
  File.ls!(dir_path) |> Enum.map(&Path.join(dir_path, &1))
end
于 2014-03-24T12:37:04.673 回答