Load file with a relative path

冷暖自知 提交于 2019-12-03 07:11:04

When a file is being loaded, the variable *LOAD-PATHNAME* is bound to the pathname of the file being loaded, and *LOAD-TRUENAME* to its truename.

So, to load a file in the same directory with the file currently being loaded, you can say

(load (merge-pathnames "main.lisp" *load-truename*))

jlahd's answer is excellent.

If you need to make different pathname calculations, you can do it with the built-in functions:

(let* ((p1    (pathname "test.lisp"))   ; not fully specified
       (name1 (pathname-name p1))       ; the name "test"
       (type1 (pathname-type p1))       ; the type "lisp"
       (p2 #p"/Users/joswig/Documents/bar.text")  ; a complete pathname
       (dir2  (pathname-directory p2))) ; (:ABSOLUTE "Users" "joswig" "Documents")

  ; now let's construct a new pathname

  (make-pathname :name name1
                 :type type1
                 :directory (append dir2 (list "Lisp"))   ; we append a dir
                 :defaults p2))         ; all the defaults
                                        ; relevant when the filesystem supports
                                        ; host, device or version

The result: #P"/Users/joswig/Documents/Lisp/test.lisp".

Usually to reuse something like above, one turn it into a utility function...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!