1. Path模块
Path.basename() ---文件名+扩展名
Path.dirname() ---路径名
Path.extname() ---扩展名
Path.join() ---拼接路径
Path.resolve() ---拼接路径,返回绝对路径
Path.parse() ---返回对象,解析路径
Path.format() ---parse的逆运算
Path.isAbsolute() ---判断是否是绝对路径
Path.normalize() ---把路径标准化
Path.relative() ---fron到to的相对路径,某个目录到某个目录
Path.delimiter ---输出分号;
Path.sep ---输出反斜杠\
Path.posix ---规范输出路径,用的是斜杠
Path.win32 ---
// 导入path核心模块
const path = require('path');
// 处理路径信息
let relativePath = '../流操作/file/1.txt';
let absolutePath = 'D:/XAMP/htdocs/node/流操作/file/2.txt';
// 类Unix系统 /users/home/abc/readme.md
console.log('basename:'+path.basename(relativePath));//输出文件名:1.txt
console.log('basename:'+path.basename(relativePath,'.txt'));//输出文件名称,后缀去掉,即 1
console.log('dirname:'+path.dirname(relativePath));//输出前面部分:../流操作/file
console.log('extname:'+path.extname(relativePath));//后缀名:.txt
console.log('join:'+path.join('../node/new','1.html'));//..\node\new\1.html
console.log('resolve:'+path.resolve('../node/new','1.html'));//输出绝对路径:D:\XAMP\htdocs\node\node\new\1.html
console.log(path.parse(relativePath));
// 输出对象:root为空
//{ root: '',
// dir: '../流操作/file',
// base: '1.txt',
// ext: '.txt',
// name: '1' }
console.log(path.parse(absolutePath));
//输出:root为D
// { root: 'D:/',
// dir: 'D:/XAMP/htdocs/node/流操作/file',
// base: '2.txt',
// ext: '.txt',
// name: '2' }
console.log(path.format({dir:'../node/流操作','base':'1.txt'}));//输出:../node/流操作\1.txt
console.log(path.isAbsolute(relativePath));//false
console.log(path.isAbsolute(absolutePath));//true
console.log(path.normalize(relativePath));//..\流操作\file\1.txt
console.log(path.normalize(absolutePath));//D:\XAMP\htdocs\node\流操作\file\2.txt
console.log(path.relative('D:/XAMP/htdocs/','D:/2.txt'));//..\..\2.txt
console.log(path.delimiter);//输出分号;
console.log(path.sep);//输出反斜杠\
// 不论操作系统,同意按照posix的方式
console.log(path.posix.normalize(relativePath));//输出:../流操作/file/1.txt