博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
叶节点到根节点的路径_节点路径模块
阅读量:2510 次
发布时间:2019-05-11

本文共 3469 字,大约阅读时间需要 11 分钟。

叶节点到根节点的路径

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') //somethingrequire('path').basename('/test/something.txt') //something.txtrequire('path').basename('/test/something.txt', '.txt') //something

path.dirname() (path.dirname())

Return the directory part of a path:

返回路径的目录部分:

require('path').dirname('/test/something') // /testrequire('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') // truerequire('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'

翻译自:

叶节点到根节点的路径

转载地址:http://zoqgb.baihongyu.com/

你可能感兴趣的文章
第八十节,CSS3边框图片效果
查看>>
第一百九十五节,jQuery EasyUI,Resizable(调整大小)组件
查看>>
Gym 101128F Landscaping(网络流)题解
查看>>
使用Expression进行查询拼接
查看>>
父页面获得子页面的值
查看>>
elment 中 el-table 进行校验
查看>>
SQL server 动态查询(表名或字段动态),并且获取想得到的返回值结果
查看>>
Nginx配置详解
查看>>
突袭HTML5之WebGL 3D概述(上) - WebGL原生开发
查看>>
SQL 映射的 XML 文件
查看>>
转:如何成为Linux高手
查看>>
Oracle数据库修改LISTENER的监听端口
查看>>
jvm 监控工具
查看>>
java的注释和分隔符
查看>>
Vue中scoped css和css module比较
查看>>
String类的写法
查看>>
数据结构常见的八大排序算法(详细整理)
查看>>
phpStudy设置多站点
查看>>
你知道微视背后的视频特效技术是怎样做出来的吗?
查看>>
在 Sublime Text 3 中使用 SublimeClang 插件
查看>>