clisp 文件path

make-pathname 创建路径名

路径名包含了六个部分:host(主机)、device(设备)、directory(目录)、name(名称)、type(类型) 及 version(版本)
default-pathname-defaults 决定了生成路径的根路径
也更具执行脚本时所在的目录

(defparameter *path* (make-pathname
               :directory '(:relative "a" "b")
               :name "main"
               :type "lisp"))

(print *path*) ; #P"a\\b\\main.lisp"

;; 带盘符
(defparameter *path* (make-pathname
               :device "F"
               :directory '(:absolute "a" "b")
               :name "main"
               :type "lisp"))
(print *path*) ; #P"F:\\a\\b\\main.lisp"

(defparameter *path* (make-pathname
               :directory '(:absolute "a" "b")
               :defaults "main.ts"))
(print *path*) ; #P"\\a\\b\\main.ts"

pathname

更具本地文件名语法解析

(pathname "./a/b/main.txt") ; #P"a\\b\\main.txt"   路径名叙述符
(namestring  #P"a\\b\\main.txt") ; "a\\b\\main.txt" 返回字符传
(directory-namestring  #P"a\\b\\main.txt") ; "a\\b\\"  返回路径的字符串
(file-namestring  #P"a\\b\\main.txt") ; "main.txt"  返回name+type

(pathname-name (pathname "./a/b/main.lisp")) ; main
(pathname-type (pathname "./a/b/main.lisp")) ; lisp
(pathname-directory (pathname "./a/b/main.lisp")) ; (:RELATIVE "a" "b") 相对路径
pathname-host, pathname-device,  返回驱动器字符
pathname-version

合并路径,反转路径

(merge-pathnames #p"dist/index.html" #p"dist/index.html" #p"/www/html/") ; #P"\\www\\html\\dist\\index.html"

(enough-namestring #p"dist/index.html" #p"dist/") ; "index.html"

文件,目录是否存在

不存在返回nil,存在则返回完整的路径

(print (probe-file #p"./as"))

在那个目录执行的脚本

他并不是 __dirname,执行程序所在的文件位置

(directory "./")

更改目录名, 文件名

;; a.txt => aa.ts 若文件已存在则报错
(rename-file "./a.txt" "aa.ts")

删除文件

(delete-file "./aa.ts")

猜你喜欢

转载自www.cnblogs.com/ajanuw/p/9052763.html
今日推荐