美文网首页
fs.realpath把文件或目录的相对路径解析为绝对路径

fs.realpath把文件或目录的相对路径解析为绝对路径

作者: 静昕妈妈芦培培 | 来源:发表于2021-02-21 08:35 被阅读0次

    例1:

    const fs = require('fs')
    
    //用来把文件/目录的相对路径异步的解析为真实的绝对路径
    fs.realpath('./', (err, resolvedPath) => {
        if(err) {
            console.error(err)
            return
        }
        console.log('a.text的绝对路径是:')
        console.log(resolvedPath)
    })
    

    执行结果:


    image.png

    例2:

    const fs = require('fs')
    
    //用来把文件/目录的相对路径异步的解析为真实的绝对路径
    fs.realpath('./', 'buffer', (err, resolvedPath) => {
        if(err) {
            console.error(err)
            return
        }
        console.log('a.text的绝对路径是:')
        console.log(resolvedPath)
    })
    

    执行结果:


    image.png

    例3:

    const fs = require('fs')
    
    //用来把文件/目录的相对路径异步的解析为真实的绝对路径
    fs.realpath('./', {encoding: 'buffer'}, (err, resolvedPath) => {
        if(err) {
            console.error(err)
            return
        }
        console.log('a.text的绝对路径是:')
        console.log(resolvedPath)
    })
    

    执行结果:


    image.png

    例4:

    const fs = require('fs')
    
    //用来把文件/目录的相对路径异步的解析为真实的绝对路径
    fs.realpath.native('./foo', (err, resolvedPath) => {
        if(err) {
            console.error(err)
            return
        }
        console.log('foo的绝对路径是:')
        console.log(resolvedPath)
    })
    

    执行结果:


    image.png

    例5:

    const fs = require('fs')
    
    //用来把文件/目录的相对路径异步的解析为真实的绝对路径
    fs.realpath.native('./foo', 'buffer', (err, resolvedPath) => {
        if(err) {
            console.error(err)
            return
        }
        console.log('foo的绝对路径是:')
        console.log(resolvedPath)
    })
    

    执行结果:


    image.png

    例6:

    const fs = require('fs')
    
    //用来把文件/目录的相对路径同步的解析为真实的绝对路径
    const resolvedPath = fs.realpathSync('./a.text')
    console.log(resolvedPath)
    
    const resolvedPath1 = fs.realpathSync('./a.text', 'buffer')
    console.log(resolvedPath1)
    
    const resolvedPath2 = fs.realpathSync.native('./foo')
    console.log(resolvedPath2)
    
    const resolvedPath3 = fs.realpathSync.native('./foo', 'buffer')
    console.log(resolvedPath3)
    

    执行结果:


    image.png

    相关文章

      网友评论

          本文标题:fs.realpath把文件或目录的相对路径解析为绝对路径

          本文链接:https://www.haomeiwen.com/subject/rrbffltx.html