- Node.js v14.16.1 Documentation
- #">Path#
- # windows和POSIX的比较">Windows vs. POSIX# windows和POSIX的比较
- #">
path.basename(path[, ext])# - #">
path.delimiter# - #">
path.dirname(path)# - #">
path.extname(path)# - #">
path.format(pathObject)# - #">
path.isAbsolute(path)# - #">
path.join([...paths])# - #">
path.normalize(path)# - #">
path.parse(path)# - #">
path.posix# - #">
path.relative(from, to)# - #">
path.resolve([...paths])# - #">
path.sep# - #">
path.toNamespacedPath(path)# - #">
path.win32#
Node.js v14.16.1 Documentation
Table of Contents目录
-
- Windows vs. POSIX
path.basename(path[, ext])path.delimiterpath.dirname(path)path.extname(path)path.format(pathObject)path.isAbsolute(path)path.join([...paths])path.normalize(path)path.parse(path)path.posixpath.relative(from, to)path.resolve([...paths])path.seppath.toNamespacedPath(path)path.win32Path#
Stability: 2 - Stable
Source Code: lib/path.js
Thepathmodule provides utilities for working with file and directory paths. It can be accessed using:
path模块提供了用于处理文件和目录路径的使用工具。通过使用下面的代码可以访问这个模块:const path = require('path');
Windows vs. POSIX# windows和POSIX的比较
The default operation of thepathmodule varies based on the operating system on which a Node.js application is running.
路径模块的默认操作会基于nodejs应用运行的操作系统而改变。
Specifically, when running on a Windows operating system, thepathmodule will assume that Windows-style paths are being used.
具体来说,在Windows操作系统上运行时,路径模块将假设正在使用Windows样式路径。
So usingpath.basename()might yield different results on POSIX and Windows:
所以使用这个方法可能得出不一样的结果在POSIX和windows上。
On POSIX: 在POSIX上:
On Windows:在windows上path.basename('C:\\temp\\myfile.html');// Returns: 'C:\\temp\\myfile.html'
To achieve consistent results when working with Windows file paths on any operating system, usepath.basename('C:\\temp\\myfile.html');// Returns: 'myfile.html'
path.win32:
当使用windows文件路径在任何操作系统上,为了达到一致的结果,使用上面蓝色的这个
On POSIX and Windows:
To achieve consistent results when working with POSIX file paths on any operating system, usepath.win32.basename('C:\\temp\\myfile.html');// Returns: 'myfile.html'
path.posix:
当使用POSIX文件路径在任何操作系统上,为了达到一致的结果,使用…
On POSIX and Windows:
On Windows Node.js follows the concept of per-drive working directory.path.posix.basename('/tmp/myfile.html');// Returns: 'myfile.html'
windows上的nodejs遵循每个驱动工作目录的概念。
This behavior can be observed when using a drive path without a backslash.
当使用驱动器路径的时候没有使用反斜杠,这种行为会被观察到。
For example,path.resolve('C:\\')can potentially return a different result thanpath.resolve('C:'). For more information, seethis MSDN page.
例如,上面这两种使用方式返回的结果可能不同。更多信息参考那个链接。
这个方法返回路径中最后的一部分,如果指定了扩展名参数,那么就直接返回文件名,扩展名参数要这样使用 .png ,这个方法会把路径中最后的 .png去掉再返回,就是一个简单的字符串处理,使用的时候要注意。path.basename(path[, ext])#
extAn optional file extension 可选的文件扩展名 - Returns:
返回值:字符串
The path.basename() method returns the last portion of a path, similar to the Unix basename command.
这个方法返回路径的最后一部分(就是最后一个斜杠之后的内容),类似于Unix系统上的basename命令。
Trailing directory separators are ignored, seepath.sep.
尾随目录的分隔符将会被忽略(就是路径最后如果还有斜杠,就会被忽略)
path.basename('/foo/bar/baz/asdf/quux.html');// Returns: 'quux.html'path.basename('/foo/bar/baz/asdf/quux.html', '.html');// Returns: 'quux'
Although Windows usually treats file names, including file extensions, in a case-insensitive manner, this function does not.
虽然window系统对待文件名,包括文件扩展的时候不区分大小写,但是这个函数是区分大小写的。
For example, C:\\foo.html andC:\\foo.HTML refer to the same file, but basename treats the extension as a case-sensitive string:
例如:上面这两个路径引用同样的文件,但是这个方法对待扩展名是大小写敏感的字符串,所以…
path.win32.basename('C:\\foo.html', '.html');// Returns: 'foo'path.win32.basename('C:\\foo.HTML', '.html');// Returns: 'foo.HTML'
A TypeError is thrown if path is not a string or if ext is given and is not a string.
如果路径参数和扩展名参数不是字符串,将会抛出类型错误异常。
path.delimiter#
Added in: v0.9.3
Provides the platform-specific path delimiter:
提供特定于平台的多个路径之间的分隔符
;for Windows:for POSIX
For example, on POSIX:
console.log(process.env.PATH); //这个返回的是环境变量// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'process.env.PATH.split(path.delimiter);// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
On Windows:
console.log(process.env.PATH);// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'process.env.PATH.split(path.delimiter);// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
上面使用相同的代码对不同操作系统的环境变量字符串分割为数组,这就是一个使用场景,这样可以让同样的代码实现跨平台。
path.dirname(path)#
The path.dirname() method returns the directory name of a path, similar to the Unix dirname command.
这个方法返回路径字符串中的目录部分,类似于…命令
Trailing directory separators are ignored, seepath.sep.
path.dirname('/foo/bar/baz/asdf/quux');// Returns: '/foo/bar/baz/asdf'
A TypeError is thrown if path is not a string.
参数不是字符串将会报错
path.extname(path)#
History
The path.extname() method returns the extension of the path, from the last occurrence of the . (period) character to end of string in the last portion of the path.
这个方法返回路径中的扩展名,在路径的最后一部分,从最后一次出现句号字符到字符串末尾。
If there is no . in the last portion of the path, or if there are no . characters other than the first character of the basename of path (see path.basename()) , an empty string is returned.
路径最后没有句号,或者basename方法返回的字符串第一个是句号之外没有其他句号,将返回空字符串。(这个说法比较绕,看下面例子就好了)
path.extname('index.html');// Returns: '.html'path.extname('index.coffee.md');// Returns: '.md'path.extname('index.');// Returns: '.'path.extname('index');// Returns: ''path.extname('.index');// Returns: ''path.extname('.index.md');// Returns: '.md'
A TypeError is thrown if path is not a string.
参数不是字符串就报错
path.format(pathObject)#
Added in: v0.11.15
