![](https://i-blog.csdnimg.cn/blog_migrate/98ded788ea5165da81db5103d88d030b.png)
path模块是node.js中处理路径的核心模块。可以很方便的处理关于文件路径的问题。
join() 将多个参数值合并成一个路径
1 2 | const path = require ( 'path' ); console.log(path.join( './a' , 'b' , 'c' )); |
basename() 获取路径中的文件名
1 2 3 4 5 | const path = require ( 'path' ); //获取文件名,包含扩展名 console.log(path. basename ( './a/b/c/1.txt' )); //获取文件名,去掉扩展名 console.log(path. basename ( './a/b/c/1.txt' , '.txt' )); |
extname() 获取路径中的扩展名
1 2 3 | const path = require ( 'path' ); //获取文件中的扩展名 console.log(path.extname( './a/b/c/1.txt' )); |
sep 返回操作系统指定的路径分割符
1 2 3 4 | const path = require ( 'path' ); console.log(path.sep); console.log(path.win32.sep); console.log(path.posix.sep); |
delimiter 返回操作系统环境变量的分割符
1 2 3 4 | const path = require ( 'path' ); console.log(path.delimiter); console.log(path.win32.delimiter); console.log(path.posix.delimiter); |
normalize() 将非标准的路径转换成标准路径,解析像 '.' ,'..',或多个杠。
1 2 3 | const path = require ( 'path' ); console.log(path.normalize( './a/b/../../c' )); console.log(path.normalize( './a//b//.././c' )); |
resolve() 返回绝对路径
1 2 | const path = require ( 'path' ); console.log(path.resolve( './a/b/../c/./d' )); |
isAbsolute() 判断是否为绝对路径
1 2 3 | const path = require ( 'path' ); console.log(path.isAbsolute( './a/b/../c/./d' )); console.log(path.isAbsolute( 'E:/a' )); |
parse() 解析一个路径,返回一个带有属性的对象
1 2 | const path = require ( 'path' ); console.log(path.parse( './a/b/../c/./d/1.txt' )); |
relative() 返回参数一到参数二的相对路径
1 2 | const path = require ( 'path' ); console.log(path.relative( './a/b/c' , './a/d' )); |