本文介绍了同步检查Node.js中是否存在文件/目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 path.existsSync很早以前就已弃用.

It doesn't require a try/catch but gives you no information about what the thing is, just that it's there. path.existsSync was deprecated long ago.

旁注:您已明确询问如何同步检查 ,因此我使用了上述功能的xyzSync版本.但是,只要有可能,使用I/O最好避免同步调用.从CPU的角度来看,调用I/O子系统要花费大量时间.请注意,调用 lstat 而不是lstatSync:

Side note: You've expressly asked how to check synchronously, so I've used the xyzSync versions of the functions above. But wherever possible, with I/O, it really is best to avoid synchronous calls. Calls into the I/O subsystem take significant time from a CPU's point of view. Note how easy it is to call lstat rather than lstatSync:

// Is it a directory?
lstat('/the/path', function(err, stats) {
    if (!err && stats.isDirectory()) {
        // Yes it is
    }
});

但是,如果您需要同步版本,就可以使用它.

But if you need the synchronous version, it's there.

以下几年前的答案现在有点过时了.当前方法是使用 fs.existsSync 进行文件/目录存在的同步检查(或者当然是 fs.exists 进行异步检查),而不是path下面的版本.

The below answer from a couple of years ago is now a bit out of date. The current way is to use fs.existsSync to do a synchronous check for file/directory existence (or of course fs.exists for an asynchronous check), rather than the path versions below.

示例:

var fs = require('fs');

if (fs.existsSync(path)) {
    // Do something
}

// Or

fs.exists(path, function(exists) {
    if (exists) {
        // Do something
    }
});

2015年2月更新

现在我们到了2015年,Node文档现在说fs.existsSync(和fs.exists)将被弃用". (因为Node员工认为在打开某个东西之前先检查是否存在某种东西是愚蠢的;但这并不是检查某个东西是否存在的唯一原因!)

Update February 2015

And here we are in 2015 and the Node docs now say that fs.existsSync (and fs.exists) "will be deprecated". (Because the Node folks think it's dumb to check whether something exists before opening it, which it is; but that's not the only reason for checking whether something exists!)

因此,我们很可能会回到各种stat方法...当然,直到/除非再次更改,否则.

So we're probably back to the various stat methods... Until/unless this changes yet again, of course.

不知道它在那里待了多久,但是还有 fs.access(path, fs.F_OK, ...)/fs.accessSync(path, fs.F_OK) .而且至少从2016年10月起, fs.stat文档建议使用fs.access来做是否存在检查(要检查文件是否存在而不随后对其进行操作,建议使用fs.access()." ).但是请注意,访问不可用被认为是错误,因此,如果您期望文件可访问,则可能是最好的选择:

Don't know how long it's been there, but there's also fs.access(path, fs.F_OK, ...) / fs.accessSync(path, fs.F_OK). And at least as of October 2016, the fs.stat documentation recommends using fs.access to do existence checks ("To check if a file exists without manipulating it afterwards, fs.access() is recommended."). But note that the access not being available is considered an error, so this would probably be best if you're expecting the file to be accessible:

var fs = require('fs');

try {
    fs.accessSync(path, fs.F_OK);
    // Do something
} catch (e) {
    // It isn't accessible
}

// Or

fs.access(path, fs.F_OK, function(err) {
    if (!err) {
        // Do something
    } else {
        // It isn't accessible
    }
});

2016年12月更新

您可以使用 fs.existsSync() :

if (fs.existsSync(path)) {
    // Do something
}

它已被弃用了几年,但现在已经不复存在了.从文档中:

It was deprecated for several years, but no longer is. From the docs:

这篇关于同步检查Node.js中是否存在文件/目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:09