elisp - 在 emacs lisp 中将多个路径组件连接成单个完整路径的正确方法是什么?

标签 elisp filenames path-manipulation

假设我有变量 dirfile 分别包含表示目录和文件名的字符串。在 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-directoryfile-name-nondirectory 以及 File Name Components 中的其他命令。部分。

关于elisp - 在 emacs lisp 中将多个路径组件连接成单个完整路径的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964715/

相关文章:

windows - 如何克隆文件名中带有冒号的文件

c# - 如何在filewatcher中更新文件名

android - 为什么 android xml 文件名必须只包含小写字母和数字 ([a-z0-9_.])?

c# - 给定一个文件系统路径,是否有更短的方法来提取不带扩展名的文件名?

c - 强化 : Path Manipulation in C - White List Implementation doesn't work - fopen issue

regex - 如何重新定义 "minibuffer"模式映射中的键?

emacs - 如何使用 Emacs 同时修改两个匹配的分隔符?

.net - Path.Combine 的安全版本

emacs - 使用邪恶键绑定(bind)时在 emacs 中按 Ctrl+U

python - Emacs:加载后评估代码始终被执行