运行节点应用程序以读取目录
fs.readdirSync('/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward');
我一直得到
Stack:
error properties: Object({ errno: -2, syscall: 'scandir', code: 'ENOENT', path: '/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward' })
Error: ENOENT: no such file or directory, scandir '/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward'
但是我知道目录在那里
ls -al /Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward
total 0
drwxr-xr-x@ 9 edwardthompson staff 288 Jan 18 2019 .
drwxr-xr-x@ 6 edwardthompson staff 192 Aug 29 12:30 ..
drwxr-xr-x@ 9 edwardthompson staff 288 Jan 18 2019 Births
drwxr-xr-x@ 4 edwardthompson staff 128 Jan 18 2019 Census
drwxr-xr-x@ 3 edwardthompson staff 96 Jan 18 2019 Deaths
drwxr-xr-x@ 7 edwardthompson staff 224 Jan 18 2019 Marriages
drwxr-xr-x@ 4 edwardthompson staff 128 Jan 18 2019 Military
drwxr-xr-x@ 3 edwardthompson staff 96 Jan 18 2019 Other
drwxr-xr-x@ 8 edwardthompson staff 256 Jan 18 2019 books
不确定如何进行
fs.lstat给我同样的ENOENT结果。 Dropbox目录具有扩展属性(找不到getfattr)
我尝试了/ user / ...,但没有任何作用
答案 0 :(得分:3)
const path = require('path');
const dirName = path.join(__dirname, '/ (../)x? /records/thompson-hayward');
(../)x?替换为../您需要多少次
fs.readdirSync(dirName).forEach(folder => {
// code here
});
答案 1 :(得分:2)
您可以尝试使用path.resolve()
:
fs.readdirSync(path.resolve(‘Users’, ‘edwardthompson’, ‘Dropbox’, ‘Apps’, ‘EvidentiaSoftware’, ‘records’, ‘thompson-hayward’));
答案 2 :(得分:1)
“ / Users / edwardthompson / Dropbox / Apps / EvidentiaSoftware / records / thompson-hayward”不是正确的路径-此路径以/开头,这是根目录。
在Linux中,。/是目录的快捷方式。
尝试类似“ ./Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward”
这是一个工作示例的链接,如果省略,则会显示类似的错误。 https://repl.it/@joshuaaguilar20/FrequentFunctionalFraction
var fs = require('fs');
fs.writeFile('./newFile.txt', 'Hello World', function(error){
if(error){
throw error;
} else {
console.log('success');
}
});
/* let wrong = fs.readdirSync("/newDir", function (err) {
console.log(err)
}) */
/*
Returns Error
Error: ENOENT: no such file or directory, scandir '/newDir'
at Object.readdirSync (fs.js:790:3)
at evalmachine.<anonymous>:11:12
at Script.runInContext (vm.js:133:20)
at Object.runInContext (vm.js:311:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at ReadStream.Readable.push (_stream_readable.js:224:10)
*/
//Correct
let x = fs.readdirSync("./newDir", function (err) {
//adding ./ allows unix to find the Dir
console.log(err)
})
console.log(x)
答案 3 :(得分:0)
您可以使用隐藏的字符在路径上进行一些诊断,甚至可以了解其含义。
如果这会创建问题路径:
let p = '/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward';
然后,您可以执行此操作以查看第一个字符是什么
console.log(p.charCodeAt(0));
或者,输出字符串中的所有字符代码:
console.log(p.split("").map(chr => chr.charCodeAt(0).toString()).join(","));
这里是可运行的代码段(该代码段不会在我的计算机上重现您的问题,但可能会在您的计算机上显示某些内容):
let p = '/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward';
let charCodes = p.split("").map(chr => chr.charCodeAt(0));
let str = charCodes.map((code, index) => {
return `<tr><td>${p.charAt(index)}</td><td>${code.toString()}</td><td> 0x${code.toString(16)}</td></tr>`
}).join("");
document.write(`<style>table.colright td:nth-child(2), table.colright th:nth-child(2), table.colright td:nth-child(3), table.colright th:nth-child(3) {
text-align: right;
}</style>`);
document.write("<table class='colright'>")
document.write("<thead><tr><th> char<br> </th><th> decimal<br>value</th><th>hex<br> value</th></tr></thead>");
document.write("<tbody>");
document.write(str);
document.write("</tbody></table>");
我唯一的有效理论(这是没有根据的猜测)是,以某种方式将开始字符串的/U
解释为仅在系统上手动指定的unicode字符的开头。应该以{{1}}作为unicode前缀,而不是\u
,但是它的行为肯定是在某种程度上误解了/U
。
仅供参考,您可能还想这样做:
/U
只是为了确保您的路径分隔符对于您的平台而言不是问题。
答案 4 :(得分:0)
一种简单的调试方法是尝试读取/ Users / {username}中的另一个目录。如果可行,您可能需要研究readdir的符号链接问题。 Dropbox可能会创建一个符号链接,该符号链接无法正确解析。
您可以使用fs.realpath()
or fs.realpathSync
获取所需目录的真实路径,然后在该真实路径上使用readdir。