多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## path ~~~ /* normalize join resolve basename extname dirname parse format sep delimiter win32 posix -->和操作系统有关 */ ~~~ ## 1.normalize使路径标准化 ~~~ const {normalize} = require('path'); console.log(normalize('user//bin//')) //输出 user\bin\ ~~~ ## 2.join拼接路径 ~~~ const {join} = require('path'); console.log(join('user','local','bin')) //输出 /user/local/bin ~~~ ## 3.将相对路径解析为绝对路径 ~~~ /* 将相对路径解析为绝对路径 */ const {resolve} = require('path'); console.log(resolve('/user/local/bin')) //输出 D:\user\local\bin ~~~ ## 4.basename,dirname,extname ~~~ /* basename -->文件名 dirname-->文件所在路径 extname-->拓展名 */ const {basename,dirname,extname} = require('path'); const filename = "/user/local/bin/input.txt"; console.log(basename(filename)) console.log(dirname(filename)) console.log(extname(filename)) //输出 input.txt /user/local/bin .txt ~~~ ## 5.parse,format ### 5.1parse ~~~ const {parse,fomat} = require("path"); const filename ="user/local/bin/package.json"; console.log(parse(filename)) //输出 { root: '', dir: 'user/local/bin', base: 'package.json', ext: '.json', name: 'package' } ~~~ ### 5.2format 会从一个对象返回一个路径字符串 ~~~ const {parse,format} = require("path"); const filename ="user/local/bin/package.json"; const ret = parse(filename); console.log(format(ret)) //输出 user/local/bin\package.json ~~~ ## 6.sep delimiter win32 posix (和操作系统相关) ~~~ sep -->路径的分割符 delimiter -->路径结束的分割符 win32 -->表示window posix -->mac下 ~~~ ~~~ /* delimiter -->分隔符 */ const {sep,delimiter,win32,posix} = require('path'); console.log("sep:",win32.sep); console.log("path",process.env.PATH) console.log("delimiter:",posix.delimiter) //输出 sep: \ path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Python37;C:\Users\Administrator\Desktop\python\工具\Java\jdk1.8.0_131\bin;;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\nodejs\;D:\Python37\Scripts\;D:\Python37\;C:\Program Files (x86)\Microsoft VS Code\bin;C:\Users\Administrator\AppData\Roaming\npm delimiter: : ~~~