查找文件并打印完整路径

时间:2019-05-13 22:25:03

标签: javascript filepath

我的项目文件夹结构如下:

tests
  - features
  - payloads
     - request
        -JSONRequest (create-req.json file is present inside this folder)
        -XMLRequest (sample-req.xml file is present inside this folder)

我必须基于找到create-req.json文件来打印完整的PATH

我创建了一个名为requestDataPath的变量,该变量存储直到“ request”结构的路径。并且我根据作为参数传递的值来选择我的json请求。而且我正在尝试根据文件所在的文件夹来打印完整路径。

let path = require('path');
let requestDataPath = path.resolve('tests/payloads/request/../');

let requestBodyPath = requestDataPath + '/' + 'create' + '-req.json';

console.log('Path is-->' + requestBody);

实际结果:/Users/pranaytgupta/Documents/pranay/api-automation-framework/tests/payloads/request/create-req.json

预期结果:/Users/pranaytgupta/Documents/pranay/api-automation-framework/tests/payloads/request/JSONRequest/create-req.json

1 个答案:

答案 0 :(得分:0)

如果您是从主目录运行此程序,请尝试:

  const path = `${__dirname}/tests/payloads/request/JSONRequest/create-req.json`
  console.log('path is -->', path);