如何检查文件的存在?
在模块的文档中fs是对rhe方法fs.exists(path,callback)
的描述。但是,据我所知,它检查只存在目录。我需要检查文件!
如何才能做到这一点?
解决方法
为什么不尝试打开文件? fs.open(‘YourFile’,’a’,function(err,fd){…}
反正一分钟后搜索试试这个:
反正一分钟后搜索试试这个:
var path = require('path'); path.exists('foo.txt',function(exists) { if (exists) { // do something } }); // or if (path.existsSync('foo.txt')) { // do something }
For Node.js v0.12.x
path.exists和fs.exists都已被弃用
使用fs.stat:
fs.stat('foo.txt',function(err,stat) { if(err == null) { console.log('File exists'); } else if(err.code == 'ENOENT') { // file does not exist fs.writeFile('log.txt','Some log\n'); } else { console.log('Some other error: ',err.code); } });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。