nodejs path模块详解

  目录

nodejs的path模块用法

nodejs的path模块包含一系列处理和转换文件路径的工具集

normalize

规范化路径,处理冗余的“..”、“.”、“/”字符。发现多个斜杠时,会替换成一个斜杠。当路径末尾包含一个斜杠时,保留。Windows系统使用反斜杠

1
2
3
4
5
6
7
8
console.log(path.normalize('a/b/c/../user/bin'));// a\b\user\bin
console.log(path.normalize('a/b/c///../user/bin/'));// a\b\user\bin\
console.log(path.normalize('a/b/c/../../user/bin'));// a\user\bin
console.log(path.normalize('a/b/c/.././///../user/bin/..'));// a\user
console.log(path.normalize('a/b/c/../../user/bin/../../'));// a\
console.log(path.normalize('a/../../user/bin/../../'));// ..\
console.log(path.normalize('a/../../user/bin/../../../../'));// ..\..\..\
console.log(path.normalize('./a/.././user/bin/./'));// user\bin\

join

path.join([path1], [path2], […])
将多个路径结合在一起,并转换为规范化路径

1
2
3
4
console.log(path.join('////./a', 'b////c', 'user/'));// \a\b\c\user
console.log(path.join('./a', 'b//c', 'user'));// a\b\c\user
console.log(path.join('a', '../../', 'user/'));// ..\user\
console.log(path.join(__dirname, 'a', '../../', 'user/'));// E:\jinux\node\study\user\

resolve

从源地址 from 到目的地址 to 的绝对路径

类似在shell里执行一系列的cd命令
path.resolve(‘foo/bar’, ‘/tmp/file/‘, ‘..’, ‘a/../subfile’)
类似于:
cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd
[注意]如果某个from或to参数是绝对路径(比如 ‘E:/abc’,或是以“/”开头的路径),则将忽略之前的from参数

1
2
3
4
5
6
console.log(path.resolve('.', 'testFiles/..', 'trdLayer'));// E:\jinux\node\study\path模块\trdLayer
console.log(path.resolve('..', 'testFiles', 'a.txt'));// E:\jinux\node\study\testFiles\a.txt
console.log(path.resolve('D:', 'abc', 'D:/a'));// D:\a
console.log(path.resolve('abc', 'ok.gif'));// E:\jinux\node\study\path模块\abc\ok.gif
console.log(path.resolve('abc', '..', 'a/../subfile')); // E:\jinux\node\study\path模块\subfile
console.log(path.resolve('/a', '/b', 'c')); // E:\b\c

relative

path.relative(from, to)
获取从 from 到 to 的相对路径,可以看作 path.resolve 的相反实现

1
2
3
console.log(path.relative('C:\\\test', 'C:\\\impl\\bbb'));//..\impl\bbb
console.log(path.relative('C:/test/aaa', 'C:/bbb'));//..\..\bbb
console.log(path.relative('C:/test/aaa', 'D:/bbb'));//D:\bbb

isAbsolute

path.isAbsolute(path)
path是一个绝对路径(比如 ‘E:/abc’),或者是以“/”开头的路径,二者都会返回true

1
2
3
4
5
6
console.log(path.isAbsolute('../testFiles/secLayer'));// false
console.log(path.isAbsolute('./join.js'));// false
console.log(path.isAbsolute('temp'));// false
console.log(path.isAbsolute('/temp/../..'));// true
console.log(path.isAbsolute('E:/github/nodeAPI/abc/efg'));// true
console.log(path.isAbsolute('///temp123'));// true

dirname

path.dirname(p)
返回路径p所在的目录

1
2
3
4
console.log(path.dirname('/foo/bar/baz/asdf/a.txt')); // /foo/bar/baz/asdf
console.log(path.dirname('/foo/bar/baz/asdf/')); // /foo/bar/baz
console.log(path.dirname('C:/test/aaa')); // C:/test
console.log(path.dirname(__dirname)); // E:\jinux\node\study

basename

path.basename(p[, ext])
返回路径的最后一个部分,即文件名。参数ext为需要截掉的后缀内容

1
2
3
4
console.log(path.basename('/foo/bar/baz/asdf/a.txt')); // a.txt
console.log(path.basename('/foo/bar/baz/asdf/a.txt','.txt')); // a
console.log(path.basename('/foo/bar/baz/asdf/')); // asdf
console.log(path.basename('C:/test/aaa')); // aaa

extname

path.extname(p)
返回路径p的扩展名,从最后一个’.’到字符串的末尾。如果最后一个部分没有’.’,或者路径是以’.’开头,则返回空字符串

1
2
3
4
5
console.log(path.extname('/foo/bar/baz/asdf/a.txt')); // .txt
console.log(path.extname('/foo/bar/baz/asdf/a.txt.b')); // .b
console.log(path.extname('/foo/bar/baz/asdf/a.')); // .
console.log(path.extname('C:/test/aaa/.')); // ''
console.log(path.extname('C:/test/aaa')); // ''

sep

返回对应平台下的文件分隔符,win下为’\’,linux下为’/‘

1
2
3
console.log(path.sep); // win下为\,linux下为/
console.log('foo\\bar\\baz'.split(path.sep)); // [ 'foo', 'bar', 'baz' ]
console.log('foo/bar/baz'.split(path.sep)); // win下返回['foo/bar/baz'],但在linux系统下会返回['foo','bar','baz']

delimiter

返回对应平台下的路径分隔符,win下为’;’ linux下为’:’

1
console.log(path.delimiter); //win下为“;” linux下为“:”

总结

所有代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const path = require('path');
// 当前文件路径
console.log('当前文件夹路径', process.cwd());
console.log('-------------------------------------------------------------');
/**
* path.normalize
* 规范化路径,处理冗余的“..”、“.”、“/”字符。发现多个斜杠时,会替换成一个斜杠。当路径末尾包含一个斜杠时,保留。Windows系统使用反斜杠 
*/
console.log(path.normalize('a/b/c/../user/bin'));// a\b\user\bin
console.log(path.normalize('a/b/c///../user/bin/'));// a\b\user\bin\
console.log(path.normalize('a/b/c/../../user/bin'));// a\user\bin
console.log(path.normalize('a/b/c/.././///../user/bin/..'));// a\user
console.log(path.normalize('a/b/c/../../user/bin/../../'));// a\
console.log(path.normalize('a/../../user/bin/../../'));// ..\
console.log(path.normalize('a/../../user/bin/../../../../'));// ..\..\..\
console.log(path.normalize('./a/.././user/bin/./'));// user\bin\
console.log('-------------------------------------------------------------');
/**
* path.join
* path.join([path1], [path2], [...])
* 将多个路径结合在一起,并转换为规范化路径
*/
console.log(path.join('////./a', 'b////c', 'user/'));// \a\b\c\user
console.log(path.join('./a', 'b//c', 'user'));// a\b\c\user
console.log(path.join('a', '../../', 'user/'));// ..\user\
console.log(path.join(__dirname, 'a', '../../', 'user/'));// E:\jinux\node\study\user\
console.log('-------------------------------------------------------------');
/**
* path.resolve
* path.resolve([from ...], to)
* 从源地址 from 到目的地址 to 的绝对路径,类似在shell里执行一系列的cd命令
* path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
* 类似于:
* cd foo/bar
* cd /tmp/file/
* cd ..
* cd a/../subfile
* pwd
* [注意]如果某个from或to参数是绝对路径(比如 'E:/abc',或是以“/”开头的路径),则将忽略之前的from参数
*/
console.log(path.resolve('.', 'testFiles/..', 'trdLayer'));// E:\jinux\node\study\path模块\trdLayer
console.log(path.resolve('..', 'testFiles', 'a.txt'));// E:\jinux\node\study\testFiles\a.txt
console.log(path.resolve('D:', 'abc', 'D:/a'));// D:\a
console.log(path.resolve('abc', 'ok.gif'));// E:\jinux\node\study\path模块\abc\ok.gif
console.log(path.resolve('abc', '..', 'a/../subfile')); // E:\jinux\node\study\path模块\subfile
console.log(path.resolve('/a', '/b', 'c')); // E:\b\c
console.log('-------------------------------------------------------------');
/**
* path.relative
* path.relative(from, to)
* 获取从 from 到 to 的相对路径,可以看作 path.resolve 的相反实现
*/
console.log(path.relative('C:\\\test', 'C:\\\impl\\bbb'));//..\impl\bbb
console.log(path.relative('C:/test/aaa', 'C:/bbb'));//..\..\bbb
console.log(path.relative('C:/test/aaa', 'D:/bbb'));//D:\bbb
console.log('-------------------------------------------------------------');
/**
* path.dirname
* path.dirname(p)
* 返回路径p所在的目录
*/
console.log(path.dirname('/foo/bar/baz/asdf/a.txt')); // /foo/bar/baz/asdf
console.log(path.dirname('/foo/bar/baz/asdf/')); // /foo/bar/baz
console.log(path.dirname('C:/test/aaa')); // C:/test
console.log(path.dirname(__dirname)); // E:\jinux\node\study
console.log('-------------------------------------------------------------');
/**
* path.basename
* path.basename(p[, ext])
* 返回路径的最后一个部分,即文件名。参数ext为需要截掉的后缀内容
*/
console.log(path.basename('/foo/bar/baz/asdf/a.txt')); // a.txt
console.log(path.basename('/foo/bar/baz/asdf/a.txt','.txt')); // a
console.log(path.basename('/foo/bar/baz/asdf/')); // asdf
console.log(path.basename('C:/test/aaa')); // aaa
console.log('-------------------------------------------------------------');
/**
* path.extname
* path.extname(p)
* 返回路径p的扩展名,从最后一个'.'到字符串的末尾。如果最后一个部分没有'.',或者路径是以'.'开头,则返回空字符串
*/
console.log(path.extname('/foo/bar/baz/asdf/a.txt')); // .txt
console.log(path.extname('/foo/bar/baz/asdf/a.txt.b')); // .b
console.log(path.extname('/foo/bar/baz/asdf/a.')); // .
console.log(path.extname('C:/test/aaa/.')); // ''
console.log(path.extname('C:/test/aaa')); // ''
console.log('-------------------------------------------------------------');
/**
* path.sep
* 返回对应平台下的文件分隔符,win下为'\',linux下为'/'
*/
console.log(path.sep); // win下为\,linux下为/
console.log('foo\\bar\\baz'.split(path.sep)); // [ 'foo', 'bar', 'baz' ]
console.log('foo/bar/baz'.split(path.sep)); // win下返回['foo/bar/baz'],但在linux系统下会返回['foo','bar','baz']
console.log('-------------------------------------------------------------');
/**
* path.delimiter
* 返回对应平台下的路径分隔符,win下为';' linux下为':'
*/
console.log(path.delimiter); //win下为“;” linux下为“:”
console.log('-------------------------------------------------------------');
/**
* path.isAbsolute
* path.isAbsolute(path)
* path是一个绝对路径(比如 'E:/abc'),或者是以“/”开头的路径,二者都会返回true
*/
console.log(path.isAbsolute('../testFiles/secLayer'));// false
console.log(path.isAbsolute('./join.js'));// false
console.log(path.isAbsolute('temp'));// false
console.log(path.isAbsolute('/temp/../..'));// true
console.log(path.isAbsolute('E:/github/nodeAPI/abc/efg'));// true
console.log(path.isAbsolute('///temp123'));// true
console.log('-------------------------------------------------------------');