叶节点到根节点的路径_节点路径模块

叶节点到根节点的路径

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 root

    root :根

  • dir: the folder path starting from the root

    dir :从根开始的文件夹路径

  • base: the file name + extension

    base :文件名+扩展名

  • name: the file name

    name :文件名

  • ext: the file extension

    ext :文件扩展名

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'

翻译自: https://flaviocopes.com/node-module-path/

叶节点到根节点的路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值