009path模块

    path模块

            参考:http://nodejs.cn/api/path.html

        1. path 模块提供了用于处理文件和目录的路径的实用工具

        2. path 模块的默认操作因运行 Node.js 应用程序的操作系统而异。

        3. 具体来说,当在 Windows 操作系统上运行时,path 模块将假定正在使用 Windows 风格的路径。

 

    方法:

        // 1. 返回 path 的最后一部分, 即获取某个文件夹的名字或者文件的文件名及其后缀名

        // path.basename(path[, ext])
        // 参数:
        //      path <string>
        //      ext <string> 可选的文件扩展名
        var out = path.basename('/css/dhf/tfg/carr/jdiedfrfgrtgr')

 

        // 2. 提供特定于平台的路径定界符

        // var out = path.delimiter

 

        // 3. path.dirname() 方法返回 path 的目录名

        var out = path.dirname('/foo/bar/baz/asdf/quux');
        // 返回: '/foo/bar/baz/asdf'

 

        // 4. 返回 path 的扩展名,即 path 的最后一部分中从最后一次出现的 .(句点)字符到字符串的结尾。

        var out = path.extname('index.coffee.md')
        // 返回:.md

 

        // 5. 从对象返回路径字符串。 这与 path.parse() 相反。

        var out = path.format({
            root: '/ignored',
            dir: '/home/user/dir',
            base: 'file.txt'
          });
        // 返回: '/home/user/dir/file.txt'

 

        // 6. 确定 path 是否为绝对路径。

        var out = path.isAbsolute(path)

 

        // 7. 使用特定于平台的分隔符作为定界符将所有给定的 path 片段连接在一起,然后规范化生成的路径。

        path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
        // 返回: '/foo/bar/baz/asdf'

 

        // 8. path.normalize() 方法规范化给定的 path,解析 '..' 和 '.' 片段。

        var out = path.normalize('/foo/bar//baz/asdf/quux/..');
        // 返回: '/foo/bar/baz/asdf'

 

        // 9. path.parse() 方法返回一个对象,其属性表示 path 的重要元素

        path.parse('/home/user/dir/file.txt');
        // 返回:
        // { root: '/',
        //   dir: '/home/user/dir',
        //   base: 'file.txt',
        //   ext: '.txt',
        //   name: 'file' }

 

        // 10. path.relative() 方法根据当前工作目录返回从 from 到 to 的相对路径。

        path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
        // 返回: '../../impl/bbb'

 

        // 11. path.resolve() 方法将路径或路径片段的序列解析为绝对路径。

        path.resolve('/foo/bar', './baz');
        // 返回: '/foo/bar/baz

 

posted @ 2021-11-18 16:31  CarreyB  阅读(21)  评论(0编辑  收藏  举报