Download file with relative path

I am trying to load a file in Lisp from a file in the same directory using a relative path.

My file structure is as follows:

repo/ subdir/ main.lisp test.lisp 

In main.lisp I have a number of function definitions, and in test.lisp I want to test functions.

I tried using (load "main.lisp") and (load "main") in test.lisp , as well as a number of options for the path name (i.e. including ./ to the file name), but both times I get the following error ( where <filename> is the file name passed to the download function):

File-error in function LISP::INTERNAL-LOAD: "<filename>" does not exist.

Is it possible to load main.lisp using a relative path?

It may be worth noting that I am running CMUCL and executing the code using SublimeREPL inside Sublime Text 3.

+6
source share
2 answers

When the file is downloaded, the variable *LOAD-PATHNAME* bound to the path name of the file to be downloaded and *LOAD-TRUENAME* to its name.

So, to upload a file to the same directory with the downloaded file, you can say

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

Jlahd's answer is excellent.

If you need to perform different path name calculations, you can do this with 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 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 

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

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

+4
source

Source: https://habr.com/ru/post/975171/


All Articles