假设我有变量 dir
和 file
分别包含表示目录和文件名的字符串。在 emacs lisp 中将它们加入到文件的完整路径中的正确方法是什么?
例如,如果dir
是“/usr/bin”
并且file
是“ls”
,那么我想要“/usr/bin/ls”
。但如果 dir
是 "/usr/bin/"
,我仍然想要同样的东西,没有重复的斜杠。
最佳答案
阅读 Directory Names 的手册,你会找到答案:
Given a directory name, you can combine it with a relative file name using
concat
:(concat dirname relfile)
Be sure to verify that the file name is relative before doing that. If you use an absolute file name, the results could be syntactically invalid or refer to the wrong file.
If you want to use a directory file name in making such a combination, you must first convert it to a directory name using
file-name-as-directory
:(concat (file-name-as-directory dirfile) relfile)
Don't try concatenating a slash by hand, as in
;;; Wrong! (concat dirfile "/" relfile)
because this is not portable. Always use
file-name-as-directory
.
其他有用的命令有:file-name-directory
、file-name-nondirectory
以及 File Name Components 中的其他命令。部分。
关于elisp - 在 emacs lisp 中将多个路径组件连接成单个完整路径的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964715/