问题描述
path.normalize(your_path)
和path.resolve(your_path)
之间有什么区别(如果有)?
What is the difference (if any) between path.normalize(your_path)
and path.resolve(your_path)
?
我知道path.resolve(...)
可以接受多个参数,但是具有单个参数的行为是否与调用path.normalize()
相同?
I know path.resolve(...)
can accept multiple arguments, but is the behavior with a single argument the same as calling path.normalize()
?
如果应该以相同的方式运行它们,那么当您将路径简单地传递到path.resolve(...)
时,我不明白公开path.normalize(...)
函数的目的.或者,也许,仅供参考.例如,他们在path.resolve(...)
文档中说:
If they are supposed to behave the same way, I don't understand the purpose of exposing the path.normalize(...)
function when you can simply pass the path into path.resolve(...)
Or, maybe, it's for documentation purposes. For example, they say in the documentation for path.resolve(...)
:
公开path.normalize(...)
可以更轻松地解释规范化"的含义???我不知道.
Exposing the path.normalize(...)
makes it easier to explain what "normalized" means??? I dunno.
推荐答案
path.normalize
去除了路径中多余的.
,..
等. path.resolve
将路径解析为绝对路径.示例(我的当前工作目录为/Users/mtilley/src/testing
):
path.normalize
gets rid of the extra .
, ..
, etc. in the path. path.resolve
resolves a path into an absolute path. Example (my current working directory was /Users/mtilley/src/testing
):
> path.normalize('../../src/../src/node')
'../../src/node'
> path.resolve('../../src/../src/node')
'/Users/mtilley/src/node'
换句话说,path.normalize
是我可以采用的最短路径是什么,它将把我带到与输入相同的位置",而path.resolve
是如果我走此路径,我的目的地是什么?".
In other words, path.normalize
is "What is the shortest path I can take that will take me to the same place as the input", while path.resolve
is "What is my destination if I take this path."
但是请注意,与path.resolve()
相比,path.normalize()
与上下文无关多得多.如果path.normalize()
是上下文- de pendent(即,如果考虑到当前工作目录),则上面示例中的结果将是../node
,因为这是最短的路径之一可能会从/Users/mtilley/src/testing
到/Users/mtilley/src/node
.
Note however that path.normalize()
is much more context-independent than path.resolve()
. Had path.normalize()
been context-dependent (i.e. if it had taken into consideration the current working directory), the result in the example above would've been ../node
, because that's the shortest path one could take from /Users/mtilley/src/testing
to /Users/mtilley/src/node
.
具有讽刺意味的是,这意味着path.resolve()
会以绝对术语生成相对路径(您可以在任何地方执行它,并且会产生相同的结果),而path.normalize()
会以相对术语生成绝对路径(您必须执行它相对于您要计算绝对结果的路径).
Ironically, this means that path.resolve()
produces a relative path in absolute terms (you could execute it anywhere, and it would produce the same result), whereas path.normalize()
produces an absolute path in relative terms (you must execute it in the path relative to which you want to calculate the absolute result).
这篇关于Node.js中path.normalize和path.resolve之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!