[英]Error: ENOENT: no such file or directory
我試圖從用戶的選擇中獲取文件夾路徑,對於每個文件,我都會讀取它並返回數據。 但是,在獲取文件后,由於某種我無法理解的原因而無法讀取數據。 我嘗試讀取和呈現的目錄確實存在。 我也提到了其他類似的職位。
readFolder() {
dialog.showOpenDialog({ properties: ['openDirectory'] }, (dirFiles) => {
console.log(dirFiles);
if (dirFiles === undefined) {
console.log('No file ');
return;
}
const pathName = dirFiles[0];
fs.readdir(pathName, (err, files) => {
files.forEach(file => {
fs.readFile(file, 'utf-8', (err, data) => {
if (err) {
console.log(`something went wrong ${err}`);
} else {
console.log(data);
}
});
});
});
});
}
readdir返回文件名“ a”,“ b”,“ c”等。您需要pathName + '/' + file
用於“ / path / to / file / a”
我犯的錯誤是我沒有意識到'file'的返回值,這些返回值只是文件名(字符串)而不是路徑。 設置let filePath =
$ {pathName} / $ {file} ;
然后繼續閱讀即可解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.