如何将(make-pathname:directory'(:absolute:home“directoryiwant”)转换为绝对路径

时间:2013-05-20 18:37:41

标签: directory common-lisp

我希望能够将任何操作系统上我的homedirectory中的某个目录翻译成该操作系统上的实际绝对路径,例如应该在类Unix系统上将(make-pathname :directory '(:absolute :home "directoryiwant")翻译为“/ home / weirdusername / directoryiwant”。

这样做的选择功能是什么?如

(directory-namestring 
      (make-pathname :directory '(:absolute :home "directoryiwant"))
> "~/"

实际上没有做这笔交易。

2 个答案:

答案 0 :(得分:8)

如果您需要相对于主目录的内容,Common Lisp函数user-homedir-pathnamemerge-pathnames可以帮助您:

CL-USER> (merge-pathnames 
          (make-pathname
           :directory '(:relative "directoryyouwant"))
          (user-homedir-pathname))
#P"/home/username/directoryyouwant/"

namestring functions(例如名称字符串目录名称字符串)按预期处理此路径名:

CL-USER> (directory-namestring
          (merge-pathnames 
           (make-pathname
            :directory '(:relative "directoryyouwant"))
           (user-homedir-pathname)))
"/home/username/directoryyouwant/"

答案 1 :(得分:2)

CL-USER > (make-pathname :directory (append (pathname-directory
                                              (user-homedir-pathname))
                                            (list "directoryiwant"))
                         :defaults (user-homedir-pathname))

#P"/Users/joswig/directoryiwant/"

函数NAMESTRING将其作为字符串返回。

CL-USER > (namestring #P"/Users/joswig/directoryiwant/")
"/Users/joswig/directoryiwant/"