NodeJs之Path

NodeJs版本:4.4.4

Path模块


NodeJs提供的Path模块,使得我们可以对文件路径进行简单的操作。

API


var path = require('path');
var path_str = '\\Users\\Administrator\\Desktop\\event.js';
console.log('文件名带后缀:',path.basename(path_str));
console.log('文件名不带后缀:',path.basename(path_str, '.html'));
console.log('路径分隔符:',path.delimiter); 
console.log('环境变量:',process.env.PATH); 
console.log('使用分隔符分割的环境变量:',process.env.PATH.split(path.delimiter)) //用path.delimiter分割
console.log('目录分隔符:',path.sep);
console.log('使用目录分隔符分割路径:',path_str.split(path.sep));
console.log('返回文件路径:',path.dirname(path_str));
console.log('获取文件后缀:',path.extname(path_str)); //.html
path_format = path.format({
    root : "\\",
    dir : "\\Users\\Administrator\\Desktop\\",
    base : "event.js",
    ext : ".js",
    name : "file"
});
console.log('格式化路径',path_format);
console.log('判断是否是绝对路径1:',path.isAbsolute(path_str)); 
console.log('判断是否是绝对路径2:',path.isAbsolute('/test/study/')); 
console.log('路径连接',path.join('/Users', 'Administrator', 'Desktop', 'test'));
console.log('输出规范格式的路径',path.normalize('/Users//Administrator//Desktop/'));
console.log('将路径解析成对象',path.parse(path_str));
console.log('路径解析:',path.resolve('d', 'Baidu', '..'));
//路径解析相当于执行了
// cd d && cd Baidu && cd ..
//然后返回执行后所在的路径
文件名带后缀: event.js
文件名不带后缀: event.js
路径分隔符: ;
环境变量: C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%JAVA_HOME%/bin;C:\Program Files\Java\jdk1.8.0_91\bin;D:\Program Files\nodejs\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin;D:\Program Files\Git\bin;F:\wnmp\php5;c:\Ruby23-x64\bin;C:\Users\Administrator\AppData\Roaming\npm;D:\Program Files (x86)\Microsoft VS Code\bin
使用分隔符分割的环境变量: [ 'C:\\ProgramData\\Oracle\\Java\\javapath',
  'C:\\Program Files (x86)\\Intel\\iCLS Client\\',
  'C:\\Program Files\\Intel\\iCLS Client\\',
  'C:\\Windows\\system32',
  'C:\\Windows',
  'C:\\Windows\\System32\\Wbem',
  'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\',
  'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL',
  'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL',
  'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT',
  'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT',
  'C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common',
  '%JAVA_HOME%/bin',
  'C:\\Program Files\\Java\\jdk1.8.0_91\\bin',
  'D:\\Program Files\\nodejs\\',
  'C:\\Program Files\\TortoiseSVN\\bin',
  'C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin',
  'D:\\Program Files\\Git\\bin',
  'F:\\wnmp\\php5',
  'c:\\Ruby23-x64\\bin',
  'C:\\Users\\Administrator\\AppData\\Roaming\\npm',
  'D:\\Program Files (x86)\\Microsoft VS Code\\bin' ]
目录分隔符: \
使用目录分隔符分割路径: [ '', 'Users', 'Administrator', 'Desktop', 'event.js' ]
返回文件路径: \Users\Administrator\Desktop
获取文件后缀: .js
格式化路径 \Users\Administrator\Desktop\event.js
判断是否是绝对路径1: true
判断是否是绝对路径2: true
路径连接 \Users\Administrator\Desktop\test
输出规范格式的路径 \Users\Administrator\Desktop\
将路径解析成对象 { root: '\\',
  dir: '\\Users\\Administrator\\Desktop',
  base: 'event.js',
  ext: '.js',
  name: 'event' }
路径解析: C:\Users\Administrator\Desktop\d

posted on 2016-12-10 16:11 qize 阅读(...) 评论(...) 编辑 收藏

导航

公告