關於node的path模組

2022-07-26 14:15:15 字數 2322 閱讀 8481

const path=require('path');

//basename('絕對路徑','擷取的字尾') 返回檔案的副檔名

let a=path.basename('c:\\temp\\myfile.html');

console.log('basename返回:'+a);

let b=path.basename('c:\\temp\\myfile.html','.html');

console.log('basename返回:'+b);//myfile

//dirname('資料夾路徑')返回資料夾路徑

let c=path.dirname('/foo/bar/baz/asdf/quux/index.html');

console.log('dirname返回:'+c);// 返回: '/foo/bar/baz/asdf'

//extname('檔案路徑')返回檔案的副檔名

let d=path.extname('/foo/bar/baz/asdf/quux/index.html');

console.log('extname返回:'+d)//.html

//format({})返回物件拼成的字串路徑

//物件屬性 dir ext root base name

//如果dir存在 root被忽略

//如果base存在 ext 和 name被忽略

let e=path.format();

console.log('format()返回:'+e) //format()返回:c:/myfile/資料夾.html

//isabsolute('路徑')判斷乙個路徑是否是絕對路徑 返回bool

let f1=path.isabsolute('./src/index.js');//false

let f2=path.isabsolute('/src/index.js');//true

console.log(f1,f2);

//join(字串片段)//返回字串片段拼接成的路徑

let g1=path.join('a','b','c','d');

console.log(g1);// a\b\c\d

let g2=path.join('a','b','c','d','./');

console.log(g2);// a\b\c\d\

let g3=path.join('a','b','c','d','../../');

console.log(g3);// a\b\

//normalize('路徑')用於規範化給定的路徑

let h1=path.normalize('');//返回.

console.log(h1);

let h2=path.normalize('c:\\temp\\\\foo\\bar\\..\\');

console.log(h2);// c:\temp\foo\

//pare('路徑')解析路徑 與format()相反 ************重要

let i=path.parse('c:/myfile/資料夾.html');

console.log(i);

/** *

*/ //relative(a,b)返回a對於b的相對路徑 *************重要

let j=path.relative('c:/myfile/資料夾/1.html','c:/myfile/資料夾1/2.html');

console.log(j);// ..\..\資料夾1\2.html

//resolve()將字串片段拼接成乙個絕對檔案 ************重要

let k1=path.resolve('my1','my2');

console.log(k1)//c:\users\adimn\desktop\es6\my1\my2

let k2=path.resolve();

console.log(k2);//c:\users\adimn\desktop\es6

let k3=path.resolve('a/b','../c/d');

console.log(k3);// c:\users\adimn\desktop\es6\a\c\d

let k4=path.resolve('/a/c');

console.log(k4);// c:\a\c

//sep提供路徑分隔符

let l1='a\\b\\c\\d'.split(path.sep); //window下必須這樣寫 雙反斜槓

console.log(l1); //[ 'a', 'b', 'c', 'd' ]

node內建path模組

path.extname,引數傳入需要解析的字串路徑 let strpath let info path.extname starpath console.log info jpgpath.resolve,可傳入多個字串引數,將字串拼接成路徑並將當前程式執行系統盤作為路徑起點 c lala baid...

node路徑模組path

console.log filename 兩個槓,當前正在執行的指令碼名稱 console.log dirname 當前正在執行指令碼的目錄名稱 let path require path let strpath e share node node demo6 index1.js console.l...

node中的path模組

path為nodejs的核心模組之一,主要用來處理檔案的路徑。當使用時需要引入path模組 var path require path 這裡總結一下path的屬性和方法 path.normalize src 規範化路徑,多個斜槓會被替換成乙個 路徑末尾的斜槓會被保留 windows 系統上,會使用反...