gpt4 book ai didi

windows - 在临时目录中创建文件时的“The process cannot access the file because it is being used by another process”

转载 作者:行者123 更新时间:2023-12-03 11:39:22 27 4
gpt4 key购买 nike

我在Windows上始终遇到以下错误(本地和Github Actions内部)。我在Mac OS和Linux上执行而不是时出现此错误。

Error: Custom { kind: Other, error: PathError { path: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\.tmpi45reT", err: Os { code: 32, kind: Other, message: "The process cannot access the file because it is being used by another process." } } }
代码基本上是使用 tempfile crate 创建一个临时目录,然后在该目录中执行一堆 File::create(以及其他一些可能不相关的东西)。写入所有文件后,将对每个文件进行显式 drop ped,而对目录句柄进行显式 close d。这是代码的粗略版本:
Rust playground
use fs::File;
use std::env;
use std::fs;
use std::io::Write as _;
use tempfile::tempdir;

fn main() -> std::io::Result<()> {
let dir = tempdir()?;
let dir_path = dir.path();
let pkg_path = dir_path.join("package.json");

let mut pkg_json = File::create(pkg_path)?;

pkg_json.write_all(
r#"
{
"name": "testpackage"
}
"#
.as_bytes(),
)?;

let _a = File::create(dir_path.join("yarn.lock"))?;
let _b = File::create(dir_path.join(".gitignore"))?;

env::set_current_dir(&dir)?;

drop(pkg_json);

drop(_a);
drop(_b);

dir.close()?;

Ok(())
}
这是集成测试的一部分,其中每个测试都执行与上面的代码相似的操作。
是什么导致此错误?

最佳答案

我发现原因是在将当前目录手动设置为tmp目录后,没有将当前目录改回原来的位置。

关于windows - 在临时目录中创建文件时的“The process cannot access the file because it is being used by another process”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63684982/

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