gpt4 book ai didi

javascript - 在 JavaScript 中重命名返回 "EPERM: operation not permitted"

转载 作者:搜寻专家 更新时间:2023-11-01 00:06:04 25 4
gpt4 key购买 nike

在我的 windows 服务器中,我发现 fs.rename 函数总是返回以下错误:

error code is Error: EPERM: operation not permitted, rename 'C:\javascript\nodejs\a.txt' -> 'C:\javascript\nodejs\b.txt'

下面是测试代码:

var fs = require('fs');

fs.writeFileSync('a.txt',"This is a file")
fs.writeFileSync('b.txt',"This is another file")

fs.rename('a.txt','b.txt',function (err) {
console.log("error code is " + err);
});

var text = fs.readFileSync('b.txt', "utf-8");

console.log(text)

但是,在当前文件夹中,我确实看到了原始文件“a.txt”以及新重命名的文件“b.txt”。

最佳答案

fs.rename只是 rename 的包装器,根据 rename docs :

EPERM or EACCES
The directory containing oldpath has the sticky bit (S_ISVTX)
set and the process's effective user ID is neither the user ID
of the file to be deleted nor that of the directory containing
it, and the process is not privileged (Linux: does not have
the CAP_FOWNER capability); or newpath is an existing file and
the directory containing it has the sticky bit set and the
process's effective user ID is neither the user ID of the file
to be replaced nor that of the directory containing it, and
the process is not privileged (Linux: does not have the
CAP_FOWNER capability); or the filesystem containing pathname
does not support renaming of the type requested.

您可能没有删除文件的权限,或者目标文件名已经存在。

关于javascript - 在 JavaScript 中重命名返回 "EPERM: operation not permitted",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com