【问题标题】:Can fs.unlink() delete a empty or non empty folder?fs.unlink() 可以删除空文件夹或非空文件夹吗?
【发布时间】:2017-02-19 05:03:54
【问题描述】:

我是 Node.js 的新手。

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});

这是我从 node.js 文档文件系统介绍示例中复制的一些代码。

但是,我很困惑。 unlink()可以删除文件夹吗?

我试过了,还是不行。

那么unlink()可以删除文件夹吗?

【问题讨论】:

    标签: javascript node.js fs


    【解决方案1】:

    fs.unlink(path, callback) 函数用于删除文件而不是文件夹。

    要删除文件夹,您可以改用fs.rmdir(path, callback) 函数。

    【讨论】:

    • 如果我取消链接文件,那么它当然会从目标文件夹中删除
    • 你也可以在这里看到stackoverflow.com/questions/18052762/… SharpCoder 的回答
    • 它也说同样的unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse.我的话只是尊重
    • @skypjack 这是吹毛求疵还是试图混淆视听?如果不删除文件,您能更具体地说明 unlink 的作用吗?你的 cmets 看起来像是廉价的拖钓。
    猜你喜欢
    • 2010-09-23
    • 2020-07-23
    • 2014-02-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    相关资源
    最近更新 更多