【问题标题】:How to provide a correct path when using run-program使用运行程序时如何提供正确的路径
【发布时间】:2021-03-03 15:16:09
【问题描述】:

例如,我有一堆这样的文件名:

[foo

我正在编写一些代码来收集它们并对其进行一些处理。

(setf a (car (uiop:directory-files "/path/to/dir")));;for simplicity
                                              ;;we suppose there is only a [foo at /path/to/dir
(uiop:run-program (list "cat" (namestring a)));; cat is just an example

然后它是这样说的:

 Subprocess #<UIOP/LAUNCH-PROGRAM::PROCESS-INFO {1009C93CA3}>
 with command ("cat" "/path/to/dir/\\[foo")
 exited with error code 1

虽然(uiop:run-program (list "cat" "/path/to/dir/\[asd")) 看起来不错。

我也试过这样的:

(format NIL "~A" a)
(format NIL "~S" temp)
(princ-to-string temp)

那么如何使用正确的路径名调用运行程序?

【问题讨论】:

    标签: common-lisp sbcl uiop


    【解决方案1】:

    对于 SBCL,真实名称是可读的,SBCL 允许在其路径名中使用方括号作为通配符,因此它需要在打印的表示中对它们进行转义。例如:

    #P"d[0-9]"
    

    有一个pathname-name,即:

    #<SB-IMPL::PATTERN "d" (:CHARACTER-SET . "0-9")>
    

    当我测试您的示例并检查路径名时,我看到:

    A pathname.                                                                                                                                                                                                                                                             
    Namestring: "/tmp/\\[a"                                                                                                                                                                                                                                                 
    Host: #<SB-IMPL::UNIX-HOST {100010D983}>                                                                                                                                                                                                                                
    Device: NIL                                                                                                                                                                                                                                                             
    Directory: (:ABSOLUTE "tmp")                                                                                                                                                                                                                                            
    Name: "[a"                                                                                                                                                                                                                                                              
    Type: NIL                                                                                                                                                                                                                                                               
    Version: :NEWEST                                                                                                                                                                                                                                                        
    Truename: #P"/tmp/\\[a"   
    

    所以,组件是可以的:路径名不包含括号,但路径名需要用括号打印才能可读。

    不要求以直接作为本机文件名的方式打印路径名,但在 SBCL 中有一个本机文件名接口,documented here

    或者,您可以拨打(uiop:unix-namestring file)

    【讨论】:

    • 是的,我想这就是我需要的,谢谢!顺便说一句,是否有任何本地函数可以更改文件类型?
    • 我不确定我是否理解,您可以使用 (merge-pathnames (make-pathname :type new-type) 模板) 构建具有修改类型的新路径名。例如 (merge-pathnames (make-pathname :type "x") #P"a.y") 是 #P"a.x"。但是为了对文件系统产生影响,您可能需要使用rename-file。此外,如果您需要 posix 接口的接口,请查找“osicat”库。
    • 是的,这就是我需要的。再次感谢您!
    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多