在OCaml中,我如何获得canonical file path,相当于Java的File.getCanonicalPath()
(即规范化的绝对路径)?获取规范文件路径
模块Filename
具有dirname
函数,但它并未标准化文件路径,例如/etc/../usr
到/usr
,例如。
在OCaml中,我如何获得canonical file path,相当于Java的File.getCanonicalPath()
(即规范化的绝对路径)?获取规范文件路径
模块Filename
具有dirname
函数,但它并未标准化文件路径,例如/etc/../usr
到/usr
,例如。
JaneStreet Ocaml Core拥有Filename.realpath。
此外,谷歌为ocaml realpath
(请见C realpath(3);你可能会为它编写一些包装)
我们phat库定义normalized
相当精确。这个库对于大多数用例来说都是过度的,但是我们将它写入了与文件系统密切相关的应用程序。我们很快就会发布opam,但现在你必须做几个针脚。这是一个完整的工作示例:
$ opam pin add solvuu_build https://github.com/solvuu/solvuu_build
$ opam pin add phat https://github.com/solvuu/phat.git
$ utop
# #require "phat.pure";;
# module Phat = Phat_pure.Std;;
# Phat.(abs_dir "https://stackoverflow.com/a/../b/./c/d/" |> ok_exn |> normalize |> to_string);;
- : string = "/b/c/d"