叶节点到根节点的路径
The path
module provides a lot of very useful functionality to access and interact with the file system.
path
模块提供了许多非常有用的功能来访问文件系统并与文件系统进行交互。
There is no need to install it. Being part of the Node core, it can be used by requiring it:
无需安装。 作为Node核心的一部分,可以通过要求使用它:
const path = require('path')
This module provides path.sep
which provides the path segment separator (\
on Windows, and /
on Linux / macOS), and path.delimiter
which provides the path delimiter (;
on Windows, and :
on Linux / macOS).
该模块提供path.sep
其提供路径段分隔符( \
Windows上,和/
在Linux / MACOS)和path.delimiter
其提供路径定界符( ;
在Windows,和:
在Linux / MACOS)。
These are the path
methods:
这些是path
方法:
path.basename()
(path.basename()
)
Return the last portion of a path. A second parameter can filter out the file extension:
返回路径的最后一部分。 第二个参数可以过滤掉文件扩展名:
require('path').basename('/test/something') //something
require('path').basename('/test/something.txt') //something.txt
require('path').basename('/test/something.txt', '.txt') //something
path.dirname()
(path.dirname()
)
Return the directory part of a path:
返回路径的目录部分:
require('path').dirname('/test/something') // /test
require('path').dirname('/test/something/file.txt') // /test/something
path.extname()
(path.extname()
)
Return the extension part of a path
返回路径的扩展部分
require('path').extname('/test/something') // ''
require('path').extname('/test/something/file.txt') // '.txt'
path.isAbsolute()
(path.isAbsolute()
)
Returns true if it’s an absolute path
如果是绝对路径,则返回true
require('path').isAbsolute('/test/something') // true
require('path').isAbsolute('./test/something') // false
path.join()
(path.join()
)
Joins two or more parts of a path:
连接路径的两个或多个部分:
const name = 'flavio'
require('path').join('/', 'users', name, 'notes.txt') //'/users/flavio/notes.txt'
path.normalize()
(path.normalize()
)
Tries to calculate the actual path when it contains relative specifiers like .
or ..
, or double slashes:
当它包含类似的指定符时,尝试计算实际路径.
或..
或双斜杠:
require('path').normalize('/users/flavio/..//test.txt') ///users/test.txt
path.parse()
(path.parse()
)
Parses a path to an object with the segments that compose it:
用组成它的段分析对象的路径:
root
: the rootroot
:根dir
: the folder path starting from the rootdir
:从根开始的文件夹路径base
: the file name + extensionbase
:文件名+扩展名name
: the file namename
:文件名ext
: the file extensionext
:文件扩展名
Example:
例:
require('path').parse('/users/test.txt')
results in
结果是
{
root: '/',
dir: '/users',
base: 'test.txt',
ext: '.txt',
name: 'test'
}
path.relative()
(path.relative()
)
Accepts 2 paths as arguments. Returns the relative path from the first path to the second, based on the current working directory.
接受2个路径作为参数。 根据当前工作目录返回从第一个路径到第二个路径的相对路径。
Example:
例:
require('path').relative('/Users/flavio', '/Users/flavio/test.txt') //'test.txt'
require('path').relative('/Users/flavio', '/Users/flavio/something/test.txt') //'something/test.txt'
path.resolve()
(path.resolve()
)
You can get the absolute path calculation of a relative path using path.resolve()
:
您可以使用path.resolve()
获得相对路径的绝对路径计算:
path.resolve('flavio.txt') //'/Users/flavio/flavio.txt' if run from my home folder
By specifying a second parameter, resolve
will use the first as a base for the second:
通过指定第二个参数, resolve
将使用第一个作为第二个的基础:
path.resolve('tmp', 'flavio.txt')//'/Users/flavio/tmp/flavio.txt' if run from my home folder
If the first parameter starts with a slash, that means it’s an absolute path:
如果第一个参数以斜杠开头,则表示它是绝对路径:
path.resolve('/etc', 'flavio.txt')//'/etc/flavio.txt'
叶节点到根节点的路径