Node.js Path module notes

The Path module provides some 文件路径small tools for processing , which can be introduced in the following ways:

const path = require("path")
console.log(Object.keys(path))

After the introduction, an object is obtained. The properties (including methods) of the object are shown in the following figure:
Insert picture description here

The system I use is windows, this value records the execution results of the windows system

method

path.normalize(path)

Normalize the path and parse the'...' and'.' fragments.

console.log(path.normalize('../data//users.json')) // ..\data\users.json

path.parse(pathString)

Returns an object of path string.
The path.parse() method returns an object whose attributes represent the elements of path.

  • root root directory
  • dir directory name (not including file name)
  • base file name (including extension)
  • name file name (extension not included)
  • ext extension
console.log(path.parse('../data/users.json'))
console.log(path.parse('D:\\app\\node-app\\data\\users.json'))

Insert picture description here

path.dirname(path)

Return the directory name of path

path.basename(path[, ext])

Return the last part of the path. ext is the extension, after adding it will not return the extension

path.extname(path)

Returns the extension of the file in the path.

const path = require('path')
const p = '/data/json/users.json'
console.log('path.parse()返回路径字符串的对象:',path.parse(p))
console.log('path.dirname()返回路径中的目录名:',path.dirname(p))
console.log('path.basename()返回路径中的最后一部分:',path.basename(p))
console.log('path.basename()返回路径中的最后一部分,指定了扩展名:',path.basename(p,'.json'))
console.log('path.extname()返回路径中的文件扩展名:',path.extname(p))

Insert picture description here

path.format(pathObject)

Return the path string from the object, the function is opposite to path.parse().

These attributes have priority

  • If dir is provided, root is ignored
  • If base exists, ext and name are ignored
console.log(path.format({
    
    
  root:'root',
  dir:'/data',
  base:'users.json',
  name:'users2',
  ext:'./json2'
}))

Print out: /data\users.json

path.isAbsolute(path)

Determine whether the parameter path is an absolute path.

path.relative(from, to)

Used to convert an absolute path to a relative path, and return the relative path from from to to (based on the current working directory).

path.relative('/data/a','/data') // ..
path.relative('/data/a','/data/a') // 空字符串
path.relative('/data/a','/data/a/b') // b
path.relative('/data/a','/data/b')  // ..\b

path.join([…paths])

Used to connect paths and normalize the generated paths.

path.join('a','b','c')  // a\b\c
path.join('a','b','../c','users.json')  // a\c\users.json

path.resolve([…paths])

Resolve the sequence of paths or path fragments into absolute paths (the given path sequence is processed from right to left, and each subsequent path is preceded until an absolute path is constructed)
. Return to the current working directory without parameters

path.resolve() // D:\app\node-app(当前工作目录)
path.resolve('/data/json') // D:\data\json
path.resolve('/data/json','a') // D:\data\json\a
path.resolve('/data/json','../a') // D:\data\a
path.resolve('/data/json','/a','b') // D:\a\b
path.resolve('/data/json','/a','../b') // D:\b

Attributes

path.sep

Returns the file path separator of the platform,'\' or'/'.

path.delimiter

Platform separator,; or':'.

path.posix

Provide the above path method, but always interact in a posix compatible way.

path.win32

The path.win32 property provides the Windows implementation of the path method.
Note: On Windows, both the slash character (/) and the backslash character (\) can be used as path separators; but only the backslash (\) is used in the return value.

Guess you like

Origin blog.csdn.net/dreamingbaobei3/article/details/113500749