path_test.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. const path = require('path');
  2. const bastPath = 'Temp';
  3. const rootPath = "/User/root/Temp"
  4. const filename = 'test.txt'
  5. // resolve 用于路径拼接
  6. const fileFullPath = path.resolve(bastPath, filename)
  7. const fileFullPath2 = path.resolve(rootPath, filename)
  8. console.log(fileFullPath); // 以 当前运行文件夹为相对路径
  9. console.log(fileFullPath2); // 以 根目录 为绝对路径
  10. // 获取路径信息
  11. const TempFilePath = "/User/root/config.txt"
  12. const TempFileDir = "/User/root/config"
  13. console.log(path.dirname(TempFilePath)) // /User/root
  14. console.log(path.dirname(TempFileDir)) // /User/root
  15. console.log(path.basename(TempFilePath)) // 文件名 config.txt
  16. console.log(path.extname(TempFilePath)) // 文件后缀 .txt
  17. // join 路径拼接
  18. const TempBasePath = '/User/root'
  19. const TempFileName = 'acb.txt'
  20. console.log(path.join(TempBasePath, TempFileName))
  21. // resolve 路径拼接 对比 join resolve 会多判断判断路径字符串开头是否有 "/" "./" "../"
  22. console.log(path.resolve(TempBasePath, TempFileName))