javascript - nodejs fs 路径和 readdirSync
问题描述
我尝试在我的应用程序中使用函数 readdirsync。我想阅读我的应用程序根目录顶部的一个特殊文件夹。
development
--allure
----allure-result
--myapproot
----myapp.js
我现在想读取文件夹 allure-result 为此我首先使用: watch path = "../allure/allure-results/"
var watchpath = path.join(__alluredir , 'allure-results/');
var dirFiles1 = fs.readdirSync(watchpath);
console.log("Test");
console.log(dirFiles1.length);
然后我尝试了 watch path = "/users/myname/development/allure/allure-results/"
var watchpath = path.join(__watchdir , 'allure-results/');
var dirFiles1 = fs.readdirSync(watchpath);
console.log("Test");
console.log(dirFiles1.length);
该应用程序不会回答这两条 console.log 行。只是沉默。console.log 中没有错误。看起来 readdirSync 不能正常工作。我也试过
var dirFiles1 = await fs.readdirSync(watchpath);
文件夹里面有很多文件。也许这就是问题所在?希望任何人都可以在这里提供帮助,为什么该功能没有结果。
解决方案
这是我的文件夹结构..
项目名称
|-- app.js
|-- 包.json
|-- 实用工具
|`-- test
|-- 1.js
|-- 2.js
|-- 3.js
`
|-- 包.json
|-- 节点模块
当我想列出 utils/test/* 下的文件时
const fs=require('fs');
console.log(fs.readdirSync('./utils/test').length) >>> 3
让我知道,如果这个不适合你!
推荐阅读
- json - Postgres 函数在更新为 JSON 后显示新列值
- java - 如何检查坐标是否属于JAVA中的地理区域
- image - SVG 图像标签的 onError
- spring - RepositoryRestResource 中的查询
- google-app-engine - Google App Engine“超出软内存限制”
- node.js - angular cli安装在mac上失败
- c - 替换 SIGUSR1 信号处理程序在标准 C11 和 GNU11 中的工作方式不同
- vb.net - 如何在运行时从表单中删除所有图片框
- laravel - Laravel:找不到类“SocialiteProviders\Manager\ServiceProvider”
- nginx - Nginx 入口控制器没有为普罗米修斯提供指标