在 emacs lisp 中将多个路径组件连接成单个完整路径的正确方法是什么?
假设我有变量 dir
和 file
分别包含表示目录和文件名的字符串。 emacs lisp 中将它们加入到文件的完整路径中的正确方法是什么?
例如,如果 dir
是 "/usr/bin"
且 file
是 "ls"
,那么我想要“/usr/bin/ls”
。但如果 dir
是 "/usr/bin/"
,我仍然想要同样的东西,没有重复的斜杠。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
阅读目录名称,你会找到答案:
其他有用的命令有:
file-name-directory
、file-name-nondirectory
以及 文件名组件 部分。Reading through the manual for Directory Names, you'll find the answer:
Other commands that are useful are:
file-name-directory
,file-name-nondirectory
, and others in the File Name Components section.您可以使用
expand-file-name
来实现此目的:编辑:这只适用于绝对目录名称。我认为特雷的答案是更好的解决方案。
You can use
expand-file-name
for this:Edit: this only works with absolute directory names. I think Trey's answer is the preferable solution.
我想将多个嵌套目录加入到一条路径上。最初我使用了多个
expand-file-name
调用,如下所示:然而,这相当冗长,并且向后读取。
相反,我编写了一个功能类似于 Python 的 os.path.join 的函数:
它的工作原理如下:
I wanted to join multiple nested directories onto a path. Originally I used multiple
expand-file-name
calls, like so:However this is rather verbose, and reads backwards.
Instead I wrote a function which acts like Python's
os.path.join
:It works like so:
这个问题是在 2010 年提出的,但在撰写本文时,它是“在 elisp 中加入文件路径”等搜索的热门搜索,所以我想我应该更新答案。
自 2010 年以来,Emacs 领域发生了很大变化。这有点重复的答案,因为在下面的答案中简要提到了它,但我会稍微充实它。现在有一个用于文件交互的专用库,
f.el
:不要试图重新发明轮子。您应该使用此库进行文件路径操作。您想要的功能是
f-join
:您可能需要先安装该软件包。它应该在 MELPA 上可用。
This question was asked in 2010, but at the time of writing it's the top hit for searches like "join file paths in elisp", so I thought I'd update the answer.
Since 2010, things have moved on a lot in the world of Emacs. This is somewhat of a duplicate answer since it was mentioned briefly in an answer below, but I'll flesh it out a little. There's now a dedicated library for file interactions,
f.el
:Don't try to reinvent the wheel. You should use this library for file path manipulations. The function you want is
f-join
:You may need to install the package first. It should be available on MELPA.
对于那些在 2021 年之后提出这个问题的人。elisp 内置函数
file-name-concat
可以完成这项工作。现在简单多了。可以通过以下按键在 emacs 中找到文档:
Ch f file-name-concat
For those who come to the question after 2021. elisp builtin function
file-name-concat
would do the job. It's much simpler now.Document can be found in emacs with following keystroke:
C-h f file-name-concat <enter>
这是我使用的:
与@dbr的区别:
root
是相对的,则它不会扩展路径(参见注释joindirs
将使用以"/"
开头的 first 组件作为根。注释
许多文件处理函数(全部、大多数、???)将规范化冗余斜杠并在相对路径上调用
expand-file-name
(或类似的),所以 #2而#3 可能并不重要。Here's what I use:
Differences from @dbr's:
root
is relative (see notes)root
as the root, whereasjoindirs
will use the first component starting with"/"
as the root.Notes
Many file handling functions (all, most, ???) will normalize redundant slashes and call
expand-file-name
(or similar) on relative paths, so #2 and #3 may not really matter.如果您使用方便的文件和目录操作库
f.el
,你只需要f-join
。下面的代码适用于那些由于某种原因拒绝使用这个库的人。这与 Python 的
os.path 完全相同。加入
。string-suffix-p
不存在 Emacs 24.4之前,所以我在 在 Emacs Lisp 中检查字符串是否以后缀结尾。If you use a convenient file and directory manipulation library
f.el
, you only needf-join
. The below code is for those, who for some reason refuse to use this library.This behaves exactly as Python's
os.path.join
.string-suffix-p
doesn't exist before Emacs 24.4, so i wrote my own at Check if a string ends with a suffix in Emacs Lisp.只是为了通过 Emacs 手册的链接来完成之前所说的内容:
正如其他人之前所说,OP 问题的答案是使用
expand-file-name
。这是一个内置函数,用 C 实现,因此不需要使用任何外部库。Emacs Lisp 手册 标题为 扩展文件名的函数。
而且根据Emacs在线帮助,这个功能是在Emacs 1.6版本中引入的!所以...它应该可用!
Just to complete what was said before with a link to the Emacs manual:
As others have said before, the answer to the OP question is to use the
expand-file-name
. That is a built-in function, implemented in C and therefore does not require the use of any external library.This is described in the Emacs Lisp Manual section titled Functions that Expand Filenames.
And according to Emacs on-line help this function was introduced in version ... 1.6 of Emacs! So... it should be available!