Node.js path module [Note 3]

The path module provides the function of operating paths. The more commonly used APIs are:

API illustrate
path.resolve Absolute path of splicing specification [commonly used]
path.sep Get the path separator of the operating system
path.parse Parse the path and return the object
path.basename Get the base name of a path
path.dirname Get the directory name of the path
path.extname Get the extension of the path
//文件名:E:\3.前端项目\NodeJs学习\path.js
const fs = require('fs')    	//导入 fs 模块
const path = require('path')    //导入 path 模块

// fs.writeFileSync(__dirname + '/index.html','love'); //写入文件
console.log(__dirname + '/index.html'); 

//resolve 解决
console.log(path.resolve(__dirname , './index.html')); 
console.log(path.resolve(__dirname , 'index.html')); 
// console.log(path.resolve(__dirname , '/index.html', './test')); 

//sep 分隔符
console.log(path.sep);          //获取操作系统的路径分隔符【windows: \】【linux: /】

//parse 方法 __dirname '全局变量'
console.log(__filename);        //文件的绝对路径
let str = 'E:\\3.前端项目\\NodeJs学习\\path.js'
console.log(path.parse(str));
/*
{
    root: 'E:\\',
    dir: 'E:\\3.前端项目\\NodeJs学习',
    base: 'path.js',
    ext: '.js',
    name: 'path'
}
*/
console.log(path.basename(str));	//path.js
console.log(path.dirname(str));		//E:\3.前端项目\NodeJs学习
console.log(path.extname(str));		//.js

Guess you like

Origin blog.csdn.net/qq_41361442/article/details/130975187