Node 03-path模块

文章介绍了Node.js的path模块,包括path.resolve用于拼接规范的绝对路径,path.sep获取操作系统路径分隔符,path.parse解析路径成对象,path.basename获取基础名称,path.dirname得到目录名以及path.extname获取扩展名。通过示例代码展示了这些功能的使用。
摘要由CSDN通过智能技术生成

path模块

path 模块提供了 操作路径 的功能,我们将介绍如下几个较为常用的几个 API:

API说明
path.resolve拼接规范的绝对路径 常用
path.sep获取操作系统的路径分隔符
path.parse解析路径并返回对象
path.basename获取路径的基础名称
path.dirname获取路径的目录名
path.extname获得路径的扩展名

代码示例:

 // 导入 fs 模块
 const fs = require('fs')
 // 导入 path 模块
 const path = require('path')
 
 // 写入文件
 // fs.writeFileSync(__dirname + '/index.html', 'love')
 console.log(__dirname + '/index.html') //=>D:\Desktop\Node\code\04-path/index.html
 
 // resolve 解决问题  拼接绝对路径
 console.log(path.resolve(__dirname, './index.html')) //=>D:\Desktop\Node\code\04-path\index.html
 console.log(path.resolve(__dirname, 'index.html')) //=>D:\Desktop\Node\code\04-path\index.html
 console.log(path.resolve(__dirname, '/index.html', './test')) //=>D:\index.html\test
 
 // sep 获取路径分隔符
 console.log(path.sep) //=> window \  linux /
 
 // parse 方法  __filename  '全局变量'
 console.log(__filename) //=>文件的绝对路径 //=>D:\Desktop\Node\code\04-path\01-path.js
 // 解析路径
 let str = 'D:\\Desktop\\Node\\code\\04-path\\01-path.js'
 console.log(path.parse(str))
 
 // 获取路径基础名称
 console.log(path.basename(pathname))
 
 // 获取路径的目录名
 console.log(path.dirname(pathname))
 
 // 获取路径的拓展名
 console.log(path.extname(pathname))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

史蒂文·月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值