javascript - fs.open 没有在 nodejs 中打开所需的文件
问题描述
我只是 nodejs 的初学者,我遇到了 fs 模块。我试着写了几行代码。
var fs = require('fs');
// Open file demo.txt in read mode
fs.open('/users/Upwn/desktop/ashok_pant.txt', 'r+', function (err, f) {
if(err) throw err;
console.log('Opened!');
});
输出是“打开!” 但我的本地文件系统没有打开任何内容。这可能有什么问题?
解决方案
如果您想使用链接到文件类型的默认 OS 应用程序打开文件,Philipp Kief提供的以下答案可能就是您想要的。
您可以使用开放模块:
npm install --save open
然后在你的 Node.js 文件中调用它:
const open = require('open'); open('my-file.txt');
该模块已经包含检测操作系统的逻辑,并且它运行 > 默认程序,该程序与您的系统关联到此文件类型。
推荐阅读
- time-series - 自相关解释
- javascript - echart标题被双击时如何触发事件
- javascript - 如何延迟加载 HTML 块(避免大的 DOM 大小),同时让网络爬虫注意到延迟内容?
- ios - 执行 iOS 应用程序的硬关闭并在具有自动化功能的模拟器中以保存的状态重新打开
- excel - 如何在带有连接线的Excel散点图中按系列定义颜色
- amazon-web-services - AWS 批处理始终为每个作业启动新的 ec2 实例
- kubernetes - serviceAccount 令牌量投影 - 清单文件中“路径”中的投影令牌不正确
- apache - 如何对 ENV 变量执行搜索替换并在响应标头 (.HTACCESS) 中回显结果
- python - 如何创建一个烧瓶服务器来生成数据以输入 kafka?
- python - 在pandas中提取包含多行和多列的JSON字符串列的部分